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 #include "opennurbs.h"
18
19 ON_VIRTUAL_OBJECT_IMPLEMENT(ON_Geometry,ON_Object,"4ED7D4DA-E947-11d3-BFE5-0010830122F0");
20
ON_Geometry()21 ON_Geometry::ON_Geometry()
22 {}
23
ON_Geometry(const ON_Geometry & src)24 ON_Geometry::ON_Geometry(const ON_Geometry& src) : ON_Object(src)
25 {}
26
operator =(const ON_Geometry & src)27 ON_Geometry& ON_Geometry::operator=(const ON_Geometry& src)
28 {
29 ON_Object::operator=(src);
30 return *this;
31 }
32
~ON_Geometry()33 ON_Geometry::~ON_Geometry()
34 {}
35
BoundingBox() const36 ON_BoundingBox ON_Geometry::BoundingBox() const
37 {
38 ON_BoundingBox bbox;
39 if ( !GetBoundingBox( bbox.m_min, bbox.m_max, false ) )
40 bbox.Destroy();
41 return bbox;
42 }
43
44 ON_BOOL32
GetBoundingBox(ON_BoundingBox & bbox,ON_BOOL32 bGrowBox) const45 ON_Geometry::GetBoundingBox( // returns true if successful
46 ON_BoundingBox& bbox,
47 ON_BOOL32 bGrowBox
48 ) const
49 {
50 return GetBoundingBox( bbox.m_min, bbox.m_max, bGrowBox );
51 }
52
53 ON_BOOL32
GetBoundingBox(ON_3dPoint & boxmin,ON_3dPoint & boxmax,ON_BOOL32 bGrowBox) const54 ON_Geometry::GetBoundingBox( // returns true if successful
55 ON_3dPoint& boxmin,
56 ON_3dPoint& boxmax,
57 ON_BOOL32 bGrowBox
58 ) const
59 {
60 ON_Workspace ws;
61 const int dim = Dimension();
62 double *bmin, *bmax;
63 if ( dim <= 3 ) {
64 bmin = &boxmin.x;
65 bmax = &boxmax.x;
66 }
67 else {
68 bmin = ws.GetDoubleMemory(dim*2);
69 bmax = bmin+dim;
70 memset( bmin, 0, 2*dim*sizeof(*bmin) );
71 if ( bGrowBox ) {
72 bmin[0] = boxmin.x; bmin[1] = boxmin.y; bmin[1] = boxmin.z;
73 bmax[0] = boxmax.x; bmax[1] = boxmax.y; bmax[1] = boxmax.z;
74 }
75 }
76 // Treat invalid box on input as empty
77 bool invalid=false; //input box invalid=empty
78 if(bGrowBox)
79 invalid = boxmin.x>boxmax.x || boxmin.y>boxmax.y|| boxmin.z>boxmax.z;
80 if(bGrowBox && invalid)
81 bGrowBox=false;
82
83 const ON_BOOL32 rc = GetBBox( bmin, bmax, bGrowBox );
84 if ( dim > 3 ) {
85 boxmin.x = bmin[0]; boxmin.y = bmin[1]; boxmin.z = bmin[2];
86 boxmax.x = bmax[0]; boxmax.y = bmax[1]; boxmax.z = bmax[2];
87 }
88 else if ( dim <= 2 ) {
89 boxmin.z = 0.0;
90 boxmax.z = 0.0;
91 if ( dim <= 1 ) {
92 boxmin.y = 0.0;
93 boxmax.y = 0.0;
94 }
95 }
96 return rc;
97 }
98
GetTightBoundingBox(ON_BoundingBox & tight_bbox,int bGrowBox,const ON_Xform * xform) const99 bool ON_Geometry::GetTightBoundingBox(
100 ON_BoundingBox& tight_bbox,
101 int bGrowBox,
102 const ON_Xform* xform
103 ) const
104 {
105 // This implementation should be overridden by classes devived
106 // from ON_Geometry
107 if ( bGrowBox && !tight_bbox.IsValid() )
108 {
109 bGrowBox = false;
110 }
111 if ( !bGrowBox )
112 {
113 tight_bbox.Destroy();
114 }
115
116 if ( xform && !xform->IsIdentity() )
117 {
118 ON_3dPointArray corners(8);
119 ON_BoundingBox world_bbox;
120 if ( GetBoundingBox(world_bbox,false) )
121 {
122 world_bbox.GetCorners(corners);
123 if ( corners.GetTightBoundingBox(tight_bbox,bGrowBox,xform) )
124 bGrowBox = true;
125 }
126 }
127 else
128 {
129 if ( GetBoundingBox(tight_bbox,bGrowBox) )
130 bGrowBox = true;
131 }
132
133 return bGrowBox?true:false;
134 }
135
SwapCoordinates(int i,int j)136 ON_BOOL32 ON_Geometry::SwapCoordinates(
137 int i, int j // indices of coords to swap
138 )
139 {
140 ON_BOOL32 rc = false;
141 const int dim = Dimension();
142 if ( dim > 0 && dim <= 3 && i >= 0 && i < 3 && j >= 0 && j < 3 ) {
143 if ( i == j ) {
144 rc = true;
145 }
146 else {
147 int k;
148 ON_Xform swapij(0.0);
149 for ( k = 0; k < 4; k++ ) {
150 if ( i == k )
151 swapij[k][j] = 1.0;
152 else if ( j == k )
153 swapij[k][i] = 1.0;
154 else
155 swapij[k][k] = 1.0;
156 }
157 rc = Transform( swapij );
158 }
159 }
160 return rc;
161 }
162
Rotate(double sin_angle,double cos_angle,const ON_3dVector & axis,const ON_3dPoint & center)163 ON_BOOL32 ON_Geometry::Rotate(
164 double sin_angle, // sin(angle)
165 double cos_angle, // cos(angle)
166 const ON_3dVector& axis, // axis of rotation
167 const ON_3dPoint& center // center of rotation
168 )
169 {
170 if ( sin_angle == 0.0 && cos_angle == 1.0 )
171 return true;
172 ON_Xform rot;
173 rot.Rotation( sin_angle, cos_angle, axis, center );
174 return Transform( rot );
175 }
176
Rotate(double angle,const ON_3dVector & axis,const ON_3dPoint & center)177 ON_BOOL32 ON_Geometry::Rotate(
178 double angle, // angle in radians
179 const ON_3dVector& axis, // axis of rotation
180 const ON_3dPoint& center // center of rotation
181 )
182 {
183 if ( angle == 0.0 )
184 return true;
185 return Rotate( sin(angle), cos(angle), axis, center );
186 }
187
Translate(const ON_3dVector & delta)188 ON_BOOL32 ON_Geometry::Translate( const ON_3dVector& delta )
189 {
190 if ( delta.IsZero() )
191 return true;
192 ON_Xform tr;
193 tr.Translation( delta );
194 return Transform( tr );
195 }
196
Scale(double x)197 ON_BOOL32 ON_Geometry::Scale( double x )
198 {
199 if ( x == 1.0 )
200 return true;
201 ON_Xform s;
202 s.Scale( x, x, x );
203 return Transform( s );
204 }
205
IsDeformable() const206 bool ON_Geometry::IsDeformable() const
207 {
208 return false;
209 }
210
MakeDeformable()211 bool ON_Geometry::MakeDeformable()
212 {
213 return false;
214 }
215
ClearBoundingBox()216 void ON_Geometry::ClearBoundingBox()
217 {
218 // default implementation does nothing
219 }
220
Transform(const ON_Xform & xform)221 ON_BOOL32 ON_Geometry::Transform( const ON_Xform& xform )
222 {
223 TransformUserData(xform);
224 return true;
225 }
226
HasBrepForm() const227 ON_BOOL32 ON_Geometry::HasBrepForm() const
228 {
229 // override if specific geoemtry has brep form
230 return false;
231 }
232
BrepForm(ON_Brep * brep) const233 ON_Brep* ON_Geometry::BrepForm( ON_Brep* brep ) const
234 {
235 // override if specific geoemtry has brep form
236 return NULL;
237 }
238
239
ComponentIndex() const240 ON_COMPONENT_INDEX ON_Geometry::ComponentIndex() const
241 {
242 // default constructor sets
243 // m_type = ON_COMPONENT_INDEX::invalid_type and m_index = -1.
244 ON_COMPONENT_INDEX ci;
245 return ci;
246 }
247
EvaluatePoint(const class ON_ObjRef & objref,ON_3dPoint & P) const248 bool ON_Geometry::EvaluatePoint( const class ON_ObjRef& objref, ON_3dPoint& P ) const
249 {
250 // virtual function default
251 P = ON_UNSET_POINT;
252 return false;
253 }
254
255