ddg.datastructures.halfedge.io module
- class ddg.datastructures.halfedge.io.HalfedgeEncoder(vertex_attrs=[], edge_attrs=[], face_attrs=[], **kwargs)[source]
Bases:
JSONEncoder- default(obj)[source]
Implement this method in a subclass such that it returns a serializable object for
o, or calls the base implementation (to raise aTypeError).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
- encode(o)
Return a JSON string representation of a Python data structure.
>>> from json.encoder import JSONEncoder >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) '{"foo": ["bar", "baz"]}'
- item_separator = ', '
- iterencode(o, _one_shot=False)
Encode the given object and yield each string representation as available.
For example:
for chunk in JSONEncoder().iterencode(bigobject): mysocket.write(chunk)
- key_separator = ': '
- ddg.datastructures.halfedge.io.to_json_string(surface, vertex_attrs=[], edge_attrs=[], face_attrs=[], write_index=False)[source]
Create a json string from a given halfedge surface. Additional attributes to be included in the json string need to be specified explicitly in the respective dictionaries. All the references to vertices, edges, and faces are realized using integer indices. The
Noneface corresponds to-1- Parameters:
- surfacehalfedge surface
The surface to be converted to a json string
- vertex_attrslist of strings
vertex attributes to be contained in the json string in addition to
edge- edge_attrs
edge attributes to be contained in the json string in addition to
pre,nex,opp,head,face- face_attrs
face attributes to be contained in the json string in addition to
edge
- Returns:
- string
- ddg.datastructures.halfedge.io.write_json_file(surface, filename, vertex_attrs=[], edge_attrs=[], face_attrs=[])[source]
Create a file in json format from a given halfedge surface. Additional attributes to be included in the json string need to be specified explicitly in the respective dictionaries. All the references to vertices, edges, and faces are realized using integer indices. The
Noneface corresponds to-1- Parameters:
- surfacehalfedge surface
The surface to be converted to a json string
- filenamestring
file to be written
- vertex_attrslist of strings
vertex attributes to be contained in the json string in addition to
edge- edge_attrs
edge attributes to be contained in the json string in addition to
pre,nex,opp,head,face- face_attrs
face attributes to be contained in the json string in addition to
edge
- ddg.datastructures.halfedge.io.read_json_file(filename)[source]
Create a surface from a json file. All keys will be mapped to respective attribute of the vertices, edges, and faces.
- Parameters:
- filename: string
file containing a json representing a halfedge surface
- Returns:
- surface
- Raises:
- ValueError
If required attributes are missing in the json string
- ddg.datastructures.halfedge.io.parse_json_string(json_string)[source]
Create a surface from a json string. All keys will be mapped to respective attribute of the vertices, edges, and faces.
- Parameters:
- json_stringstring
formatted string containing a halfedge surface
- Returns:
- surface
- Raises:
- ValueError
If required attributes are missing in the json string
- class ddg.datastructures.halfedge.io.SurfaceToIFSEncoder(vertex_attrs=['co'], **kwargs)[source]
Bases:
JSONEncoder- default(surface)[source]
Implement this method in a subclass such that it returns a serializable object for
o, or calls the base implementation (to raise aTypeError).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
- encode(o)
Return a JSON string representation of a Python data structure.
>>> from json.encoder import JSONEncoder >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) '{"foo": ["bar", "baz"]}'
- item_separator = ', '
- iterencode(o, _one_shot=False)
Encode the given object and yield each string representation as available.
For example:
for chunk in JSONEncoder().iterencode(bigobject): mysocket.write(chunk)
- key_separator = ': '
- class ddg.datastructures.halfedge.io.ObjEncoder(filename)[source]
Bases:
objectClass to convert obj file to a ddg.halfedge.Surface or an IndexedFaceSet.
self.indexedfs is an IndexedFaceSet constructed of the obj file. self.halfedgeds is a Surface with halfedge attributes “vt” and “vn” (if given).
- ddg.datastructures.halfedge.io.obj_to_ifs(filename)[source]
Converts an obj file to an IndexedFaceSet.
- Parameters:
- filename: (path and) name of the obj file
- Returns:
- IndexedFaceSet specified by the given obj file
- ddg.datastructures.halfedge.io.obj_to_surface(filename)[source]
- Converts an obj file to a Surface with halfedge attributes “vt” and “vn”(if given).Obj file with given v and faces of the form f 1 2 3or given v,vt and faces of the form f 1/1 2/1 3/1or given v,vn and faces of the form f 1//1 2//1 3//1or given v,vt,vn and faces of the form f 1/1/2 2/1/2 3/1/2.
- Parameters:
- filename: (path and) name of the obj file
- Returns:
- Surface specified by the given obj file
- class ddg.datastructures.halfedge.io.SurfaceToObjEncoder(surface, filename='obj_file.obj', co_attr='co')[source]
Bases:
objectClass to convert a ddg.halfedge.Surface or an IndexedFaceSet to an obj file.
self.hds encodes the given surface.
- ddg.datastructures.halfedge.io.surface_to_obj(surface, filename='obj_file.obj', co_attr='co')[source]
- Converts a ddg.halfedge.Surface to an obj file.If ‘vn’ and/or ‘vt’ are given as halfegde attributes, they will bewritten in the obj file format.The vertices need to have coordinates, by default as the attribute ‘co’.Results in Obj file with given v and faces of the form f 1 2 3or given v,vt and faces of the form f 1/1 2/1 3/1or given v,vn and faces of the form f 1//1 2//1 3//1or given v,vt,vn and faces of the form f 1/1/2 2/1/2 3/1/2.Vertices will be ordered by indices and vt and vn coordinates in increasing order.
- Parameters:
- surface: ddg.halfedge.Surface
halfedge data structure to be converted
- filenamestr (default=’obj_file.obj’)
Name for the resulting obj file a file path can be specified here default is ‘obj_file’ which will be saved in the cwd
- co_attrstr (default=’co’)
string of the vertx attribute that encodes the coordinates
Notes
Indexing in obj files starts at 1.