opennurbs_objref.h
1 /* $NoKeywords: $ */
2 /*
3 //
4 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
5 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
6 // McNeel & Associates.
7 //
8 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
9 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
10 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
11 //
12 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
13 //
14 ////////////////////////////////////////////////////////////////
15 */
16 
17 #if !defined(ON_OBJREF_INC_)
18 #define ON_OBJREF_INC_
19 
21 {
22 public:
25 
26  void Default();
27 
28  bool Write( ON_BinaryArchive& ) const;
29  bool Read( ON_BinaryArchive& );
30 
31 
32  // If m_point != ON_3dPoint::UnsetPoint and m_t_type != 0, then
33  // m_t_type, m_t, and m_t_ci record the m_geometry evaluation
34  // parameters of the m_point.
35  //
36  // m_t_type values
37  //
38  // 0: no parameter values; m_t_ci and m_t[] have no meaning.
39  //
40  // 1: m_geometry points to a curve, m_t[0] is a curve
41  // parameter for m_point, and m_t_ci has no meaning.
42  //
43  // 2: m_geometry points to surface or single faced brep,
44  // (m_t[0],m_t[1]) is a surface parameter for m_point,
45  // and m_t_ci has no meaning.
46  // In this case, m_component_index may not be set or,
47  // if m_geometry points to a brep face, m_component_index
48  // may identify the face in the parent brep.
49  //
50  // 3: m_geometry points to a brep edge with an associated
51  // trim and m_t[0] is the edge parameter for m_point.
52  // m_t_ci is the ON_BrepTrim component index and m_t[1]
53  // is the ON_BrepTrim parameter that corresponds to the
54  // edge point. m_s[0] and m_s[1] are normalized parameters.
55  // In this case m_component_index identifies the
56  // the edge in the brep and m_t_ci identifies a trim.
57  //
58  // 4: m_geometry points to a mesh or mesh face and
59  // m_t_ci identifies the mesh face.
60  // If the face is a triangle, the barycentric coordinates
61  // of m_point are(m_t[0], m_t[1], m_t[2]) and m_t[3] is zero.
62  // If the mesh face is a quadrangle, the barycentric coordinates
63  // of m_point are (m_t[0], m_t[1], m_t[2], m_t[3]) and at least
64  // one of the coordinates is zero. In both cases, the point
65  // can be evaluated using the formula
66  // m_t[0]*mesh.m_V[f.vi[0]] + ... + m_t[3]*mesh.m_V[f.vi[3]],
67  // where f = mesh.m_F[m_component_index.m_index].
68  // In this case, if m_geometry points to a mesh, then
69  // m_component_index != m_t_ci.
70  //
71  // 5: m_geometry points to a mesh or mesh edge and m_t_ci
72  // identifies the mesh edge. The normalized coordinate of
73  // the point on the mesh edge is m_t[0]. The point can be evaluated
74  // using the formula
75  // m_t[0]*mesh.m_V[v0] + (1.0-m_t[0])*mesh.m_V[v1],
76  // where v0 and v1 are the indices of the mesh vertices at
77  // the edge's ends.
78  // In this case, if m_geometry points to a mesh, then
79  // m_component_index != m_t_ci.
80  //
81  // 6: m_geometry points to a NURBS cage and (m_t[0],m_t[1],m_t[2])
82  // are cage evaluation parameters.
83  //
84  // 7: m_geometry points to an annotation object and m_t_ci identifies
85  // a point on the annotation object.
86  //
87  // 8: m_geometry points to a mesh or mesh vertex object and m_t_ci
88  // identifies a vertex on the mesh object.
89  //
90  int m_t_type;
91 private:
92  int m_reserved; // for future use to record snap info.
93 public:
94  double m_t[4];
95  ON_Interval m_s[3]; // curve/surface/cage domains
96  ON_COMPONENT_INDEX m_t_ci; // Not necesarily the same as m_component_index
97  // See comment above for details.
98 };
99 
100 class ON_CLASS ON_ObjRef_IRefID
101 {
102 public:
103  ON_ObjRef_IRefID() = default;
104  ~ON_ObjRef_IRefID() = default;
105  ON_ObjRef_IRefID(const ON_ObjRef_IRefID&) = default;
106  ON_ObjRef_IRefID& operator=(const ON_ObjRef_IRefID&) = default;
107 
108  bool Write(ON_BinaryArchive&) const;
109  bool Read(ON_BinaryArchive&);
110 
111  void Default();
112 
113  // m_iref_uuid is the CRhinoInstanceObject's uuid stored
114  // in its ON_3dmObjectAttributes.m_uuid.
115  ON_UUID m_iref_uuid = ON_nil_uuid;
116 
117  // m_iref_xform is the value stored in ON_InstanceRef.m_xform.
119 
120  // m_idef_uuid is the instance definition id stored in
121  // ON_InstanceRef.m_instance_definition_uuid and
122  // ON_InstanceDefinition.m_uuid.
123  ON_UUID m_idef_uuid = ON_nil_uuid;
124 
125  // m_geometry_index is the index of the uuid of the pertinant
126  // piece of geometry in the ON_InstanceRef.m_object_uuid[]
127  // array. This index is identical to the index of the
128  // geometry's CRhinoObject in the
129  // CRhinoInstanceDefinition.m_objects[] array.
130  int m_idef_geometry_index = 0;
131 
132  // m_geometry_xform is the transformation to map the
133  // base geometry to world coordinates. If the
134  // instance reference is not nested, then
135  // m_geometry_xform = m_iref_xform. If the instance
136  // reference is nested, then
137  // m_geometry_xform = m_iref_xform * .... * T1
138  // where the Ts are the transformations from the children.
140 
141  // If this ON_ObjRef_IRefID is the first entry in the
142  // ON_ObjRef.m__iref[] array, then it references a "real"
143  // piece of geometry (not a nested instance reference).
144  // If the reference is to a subobject of the real piece
145  // of geometry, then m_component_index records
146  // the subobject index.
147  // In all other cases, m_component_index is not set.
148  ON_COMPONENT_INDEX m_component_index;
149 
150  // If this ON_ObjRef_IRefID is the first entry in the
151  // ON_ObjRef.m__iref[] array, then it references a "real"
152  // piece of geometry (not a nested instance reference).
153  // If there is an evaluation parameter for the geometry,
154  // it is saved in m_evp.
155  // In all other cases, m_evp is not set.
157 };
158 
159 #if defined(ON_DLL_TEMPLATE)
160 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<ON_ObjRef_IRefID>;
161 #endif
162 
163 class ON_CLASS ON_ObjRef
164 {
165 public:
166  ON_ObjRef();
167  ON_ObjRef(const ON_ObjRef& src);
168  ON_ObjRef& operator=(const ON_ObjRef& src);
169  ~ON_ObjRef();
170 
171  void Destroy();
172  bool Write( ON_BinaryArchive& ) const;
173  bool Read( ON_BinaryArchive& );
174 
175  // In Rhino, this uuid is the persistent id of the CRhinoObject
176  // that owns the referenced geometry. The value of the
177  // CRhinoObject id is stored on ON_3dmObjectAttributes.m_uuid.
179 
180  // The m_geometry and m_parent_geometry pointers are runtime values
181  // that point to the object being referenced. The destructor
182  // ~ON_ObjRef does not delete the objects these pointers reference.
183  //
184  // m_geometry_type records the type of geometry m_geometry points to.
185  //
186  // When the referenced object is a subobject, like a part of a brep
187  // or mesh, m_geometry points to the subobject and m_parent_geometry
188  // points to the parent object, like the brep or mesh. In this case
189  // m_component_index records the location of the subobject.
190  //
191  // Parts of instance reference objects:
192  // When the geometry belongs to an instance reference
193  // m_uuid is the id of the CRhinoInstanceObject,
194  // m_parent_geometry points to the instance definition
195  // geometry or a transformed proxy, and m_geometry points
196  // to the piece of m_geometry. The m__iref[] array records
197  // the connection between the instance reference and the
198  // geometry the ON_ObjRef refers to.
199  //
200  // For example if the ON_ObjRef is to an edge of a brep in
201  // and instance reference, m_uuid would be the Rhino id of
202  // the CRhinoInstanceObject, m_parent_geometry would point
203  // to a, possibly proxy, ON_Brep object, m_geometry would point
204  // to the ON_BrepEdge in the ON_Brep, m_component_index would
205  // record the edge's index in the ON_Brep.m_E[] array and
206  // m_geometry_type would be ON::curve_object or ON::brep_edge.
207  // m__iref->Last() would contain the information about the
208  // top level instance reference. If the brep was at the bottom
209  // of a chain of instance references, m__iref[0] would be the
210  // reference that immediately used the brep.
211  const ON_Geometry* m_geometry;
212  const ON_Geometry* m_parent_geometry;
213  ON_COMPONENT_INDEX m_component_index;
214  int m_geometry_type;
216  // If m_runtime_sn > 0, then it is the value of a Rhino object's
217  // CRhinoObject::m_runtime_object_serial_number field.
218  // The serial number is used instead of the pointer to
219  // prevent crashes in cases when the CRhinoObject is deleted
220  // but an ON_ObjRef continues to reference the Rhino object.
221  // The value of m_runtime_sn is not saved in archives because
222  // it generally changes if you save and reload an archive.
223  unsigned int m_runtime_sn;
225  // If m_point != ON_3dPoint::UnsetPoint, then the ObjRef resolves to
226  // a point location. The point location is saved here so the
227  // information can persist if the object itself vanishes.
228  ON_3dPoint m_point;
230  // If the point was the result of some type of object snap, then
231  // the object snap is recorded here.
232  ON::osnap_mode m_osnap_mode;
234  // If m_point != ON_3dPoint::UnsetPoint and m_evp.m_t_type != 0, then
235  // m_evp records the records the m_geometry evaluation
236  // parameters for the m_point.
239  // If m__iref[] is not empty, then m_uuid identifies
240  // and instance reference (ON_InstanceRef/CRhinoInstanceObject)
241  // and m__iref[] records the chain of instance references from
242  // the base piece of geometry to the instance reference.
243  // The top level instance reference is last in the list.
246  /*
247  Description:
248  Expert user tool to decrement reference counts. Most
249  users will never need to call this tool. It is called
250  by ~ON_ObjRef and used in rare cases when a
251  ON_ObjRef needs to reference an object only by uuid
252  and component index.
253  */
254  void DecrementProxyReferenceCount();
255 
256  /*
257  Description:
258  Expert user tool to initialize the ON_ObjRef
259  m__proxy1, m__proxy2, and m__proxy_ref_count fields.
260  */
261  void SetProxy(
262  ON_Object* proxy1,
263  ON_Object* proxy2,
264  bool bCountReferences
265  );
266 
267  bool SetParentIRef( const ON_InstanceRef& iref,
268  ON_UUID iref_id,
269  int idef_geometry_index
270  );
271 
272  /*
273  Returns:
274  0: This ON_ObjRef is not counting references.
275  >0: Number of references.
276  */
277  int ProxyReferenceCount() const;
278 
279  /*
280  Parameters:
281  proxy_object_index - [in] 1 or 2.
282  Returns:
283  A pointer to the requested proxy object.
284  */
285  const ON_Object* ProxyObject(int proxy_object_index) const;
286 
287  /*
288  Description:
289  This tool is used in rare situations when the object ids
290  stored in the uuid list need to be remapped.
291  Parameters:
292  uuid_remap - [in]
293  Is it critical that uuid_remap[] be sorted with respect
294  to ON_UuidPair::CompareFirstUuid.
295  */
296  void RemapObjectId( const ON_SimpleArray<ON_UuidPair>& uuid_remap );
297 
298 private:
299  // In simple (and the most common) cases where m_geometry
300  // is managed by something outside of the ON_ObjRef class,
301  // m__proxy_ref_count is nullptr. In this case, the m__proxy1
302  // and m__proxy2 pointers may still be used to store
303  // references to a parent object.
304  //
305  // In cases when the referenced geometry pointed at by
306  // m_geometry is not being managed by another class,
307  // m_proxy1 and m_proxy2 are not nullptr and *m_proxy_ref_count
308  // counts the number of ON_ObjRef classes that refer to m__proxy1/2.
309  // When the last ON_ObjRef is destroyed, m__proxy1/2 is deleted.
310  // When the ON_ObjRef is using reference counting and managing
311  // m__proxy1/2, m_geometry points to some part of m__proxy1/2 and
312  // m_geometry is destroyed when m__proxy1/2 is destroyed.
313  //
314  // The convention is to use m__proxy1 to store
315  // ON_MeshVertex/Edge/FaceRefs and CRhinoPolyEdges
316  // and m__proxy2 to store transformed copies if instance
317  // definition geometry.
318  ON_Object* m__proxy1;
319  ON_Object* m__proxy2;
320  int* m__proxy_ref_count;
321  //ON__INT_PTR m_reserved;
322 };
323 
324 #if defined(ON_DLL_TEMPLATE)
325 ON_DLL_TEMPLATE template class ON_CLASS ON_ClassArray<ON_ObjRef>;
326 #endif
327 
328 #endif
ON_UUID m_uuid
Definition: opennurbs_objref.h:178
ON_UUID is a 16 byte universally unique identifier.
Definition: opennurbs_uuid.h:32
static const ON_Xform ZeroTransformation
ON_Xform ZeroTransformation diagonal = (0,0,0,1)
Definition: opennurbs_xform.h:35
Base class for all geometry classes that must provide runtime class id. Provides interface for common...
Definition: opennurbs_geometry.h:37
ON_COMPONENT_INDEX m_component_index
Definition: opennurbs_objref.h:148
Definition: opennurbs_objref.h:100
Definition: opennurbs_xform.h:28
An ON_InstanceRef is a reference to an instance definition along with transformation to apply to the ...
Definition: opennurbs_instance.h:613
Pure virtual base class for all classes that must provide runtime class id or support object level 3D...
Definition: opennurbs_object.h:460
Definition: opennurbs_array.h:409
Definition: opennurbs_archive.h:1783
Definition: opennurbs_objref.h:163
Definition: opennurbs_point.h:460
Definition: opennurbs_objref.h:20
int m_t_type
Definition: opennurbs_objref.h:90
ON_COMPONENT_INDEX m_t_ci
Definition: opennurbs_objref.h:96
ON_ObjRefEvaluationParameter m_evp
Definition: opennurbs_objref.h:156
Definition: opennurbs_point.h:46