-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDM3IO.cxx
More file actions
270 lines (225 loc) · 7.55 KB
/
DM3IO.cxx
File metadata and controls
270 lines (225 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
////////////////////////////////////////////////////////////////////
//
// $Id: DM3IO.cxx 2021/06/05 12:18:12 kanai Exp $
//
// Copyright (c) 2021 Takashi Kanai
// Released under the MIT license
//
////////////////////////////////////////////////////////////////////
#include "envDep.h"
#include "DM3IO.hxx"
#include "BSPS.hxx"
namespace {
// Any brep surface that provides a NURBS form (proxy or native).
static bool tryGetNurbForm( const ON_Surface* srf, ON_NurbsSurface& nbs )
{
if ( nullptr == srf )
return false;
return srf->GetNurbForm( nbs ) > 0;
}
static void elevateNurbsForBsps( ON_NurbsSurface& nbs )
{
if ( nbs.m_is_rat )
cout << "Rational surface." << endl;
if ( nbs.m_order[0] < 4 )
nbs.IncreaseDegree( 0, 3 );
if ( nbs.m_order[1] < 4 )
nbs.IncreaseDegree( 1, 3 );
if ( ( nbs.m_order[0] == 6 ) && ( nbs.m_order[1] != 6 ) )
nbs.IncreaseDegree( 1, 5 );
if ( ( nbs.m_order[0] != 6 ) && ( nbs.m_order[1] == 6 ) )
nbs.IncreaseDegree( 0, 5 );
}
static void appendBspsPatchFromNurbs( ON_NurbsSurface& nbs, int patch_id, std::vector<BSPS>& bsps,
int& n_ucp, int& n_vcp )
{
cout << "ON_NurbsSurface dim = " << nbs.m_dim << " is_rat = "
<< nbs.m_is_rat << " order = " << nbs.m_order[0] << " X " << nbs.m_order[1]
<< " cv_count = " << nbs.m_cv_count[0] << " X " << nbs.m_cv_count[1] << endl;
cout << "Knots count =" << nbs.KnotCount( 0 ) << " X " << nbs.KnotCount( 1 ) << endl;
BSPS bsp;
bsp.setID( patch_id );
bsp.setUdegree( nbs.m_order[0] - 1 );
bsp.setVdegree( nbs.m_order[1] - 1 );
double* knot = nbs.m_knot[0];
bsp.addUkv( (float)knot[0] );
for ( int i = 0; i < nbs.KnotCount( 0 ); ++i )
bsp.addUkv( (float)knot[i] );
bsp.addUkv( (float)knot[nbs.KnotCount( 0 ) - 1] );
knot = nbs.m_knot[1];
bsp.addVkv( (float)knot[0] );
for ( int i = 0; i < nbs.KnotCount( 1 ); ++i )
bsp.addVkv( (float)knot[i] );
bsp.addVkv( (float)knot[nbs.KnotCount( 1 ) - 1] );
n_ucp = nbs.m_cv_count[0];
n_vcp = nbs.m_cv_count[1];
bsp.setNUcp( nbs.m_cv_count[0] );
bsp.setNVcp( nbs.m_cv_count[1] );
for ( int i = 0; i < nbs.m_cv_count[0]; ++i )
{
for ( int j = 0; j < nbs.m_cv_count[1]; ++j )
{
double* cv = nbs.CV( i, j );
double w = nbs.Weight( i, j );
bsp.addCp( Point4f( (float)cv[0], (float)cv[1], (float)cv[2], (float)w ) );
}
}
bsps.push_back( bsp );
cout << endl;
}
} // namespace
bool DM3IO::inputFromFile( const char* const filename, std::vector<BSPS>& bsps )
{
ON::Begin();
ON_TextLog dump_to_stdout;
ON_TextLog* dump = &dump_to_stdout;
const bool rc = model_->Read( filename, dump );
if ( rc )
dump->Print( "Successfully read.\n" );
else
dump->Print( "Errors during reading.\n" );
int id = 0;
int obj_index = 0;
ONX_ModelComponentIterator cit( *model_, ON_ModelComponent::Type::ModelGeometry );
for ( const ON_ModelComponent* mc = cit.FirstComponent(); nullptr != mc; mc = cit.NextComponent() )
{
const ON_ModelGeometryComponent* mgc = ON_ModelGeometryComponent::Cast( mc );
if ( nullptr == mgc )
continue;
const ON_Geometry* geom = mgc->Geometry( nullptr );
if ( nullptr == geom || geom->ObjectType() != ON::brep_object )
continue;
const ON_Brep* obj = ON_Brep::Cast( geom );
if ( nullptr == obj )
continue;
for ( int si = 0; si < obj->m_S.Count(); si++ )
{
const ON_Surface* srf = obj->m_S[si];
if ( nullptr == srf )
continue;
cout << "Name: " << srf->ClassId()->ClassName() << endl;
ON_NurbsSurface nbs;
if ( !tryGetNurbForm( srf, nbs ) )
{
cerr << "surface " << obj_index << " " << si
<< " has no NURBS form (skipped)." << endl;
continue;
}
elevateNurbsForBsps( nbs );
appendBspsPatchFromNurbs( nbs, id, bsps, n_ucp_, n_vcp_ );
++id;
}
++obj_index;
}
cout << "the number of patches " << bsps.size() << endl;
return rc;
}
void DM3IO::getSurfaceParameters( std::vector<Point3f>& cp,
std::vector<float>& ukv, std::vector<float>& vkv )
{
int obj_index = 0;
ONX_ModelComponentIterator cit( *model_, ON_ModelComponent::Type::ModelGeometry );
for ( const ON_ModelComponent* mc = cit.FirstComponent(); nullptr != mc; mc = cit.NextComponent() )
{
const ON_ModelGeometryComponent* mgc = ON_ModelGeometryComponent::Cast( mc );
if ( nullptr == mgc )
continue;
const ON_Geometry* geom = mgc->Geometry( nullptr );
if ( nullptr == geom || geom->ObjectType() != ON::brep_object )
continue;
const ON_Brep* obj = ON_Brep::Cast( geom );
if ( nullptr == obj )
continue;
for ( int si = 0; si < obj->m_S.Count(); si++ )
{
const ON_Surface* srf = obj->m_S[si];
if ( nullptr == srf )
continue;
ON_NurbsSurface nbs;
if ( !tryGetNurbForm( srf, nbs ) )
{
cerr << "surface " << obj_index << " " << si
<< " has no NURBS form (skipped)." << endl;
continue;
}
cout << "NURBS dim = " << nbs.m_dim << " is_rat = " << nbs.m_is_rat << " order = "
<< nbs.m_order[0] << " X " << nbs.m_order[1] << " cv_count = " << nbs.m_cv_count[0]
<< " X " << nbs.m_cv_count[1] << endl;
cout << "Knots count =" << nbs.KnotCount( 0 ) << " X " << nbs.KnotCount( 1 ) << endl;
double* knot = nbs.m_knot[0];
cout << "U dir" << endl;
ukv.push_back( (float)knot[0] );
for ( int i = 0; i < nbs.KnotCount( 0 ); ++i )
ukv.push_back( (float)knot[i] );
ukv.push_back( (float)knot[nbs.KnotCount( 0 ) - 1] );
for ( int i = 0; i < (int)ukv.size(); ++i )
cout << i << " " << ukv[i] << endl;
knot = nbs.m_knot[1];
cout << "V dir" << endl;
vkv.push_back( (float)knot[0] );
for ( int i = 0; i < nbs.KnotCount( 1 ); ++i )
vkv.push_back( (float)knot[i] );
vkv.push_back( (float)knot[nbs.KnotCount( 1 ) - 1] );
for ( int i = 0; i < (int)vkv.size(); ++i )
cout << i << " " << vkv[i] << endl;
n_ucp_ = nbs.m_cv_count[0];
n_vcp_ = nbs.m_cv_count[1];
for ( int i = 0; i < nbs.m_cv_count[0]; ++i )
{
for ( int j = 0; j < nbs.m_cv_count[1]; ++j )
{
double* cv = nbs.CV( i, j );
Point3f p( (float)cv[0], (float)cv[1], (float)cv[2] );
cp.push_back( p );
cout << i << " " << j << " " << p << endl;
}
}
}
++obj_index;
}
}
void DM3IO::normalizeCp( std::vector<BSPS>& bsps, Point3f& center, float* maxlen ) {
Point4f vmax4, vmin4;
for ( int j = 0; j < bsps.size(); ++j )
{
std::vector<Point4f>& cp = bsps[j].cp();
for ( int i = 0; i < cp.size(); ++i )
{
if ( (i == 0) && (j == 0) )
{
vmax4.set( cp[i] ); vmin4.set( cp[i] );
}
else
{
if (cp[i].x > vmax4.x) vmax4.x = cp[i].x;
if (cp[i].x < vmin4.x) vmin4.x = cp[i].x;
if (cp[i].y > vmax4.y) vmax4.y = cp[i].y;
if (cp[i].y < vmin4.y) vmin4.y = cp[i].y;
if (cp[i].z > vmax4.z) vmax4.z = cp[i].z;
if (cp[i].z < vmin4.z) vmin4.z = cp[i].z;
}
}
}
Point3f vmax( vmax4.x, vmax4.y, vmax4.z );
Point3f vmin( vmin4.x, vmin4.y, vmin4.z );
center = vmax + vmin; center.scale(.5);
Point3f len = vmax - vmin;
*maxlen = (std::fabs(len.x) > std::fabs(len.y) )
? std::fabs(len.x) : std::fabs(len.y);
*maxlen = ( *maxlen > std::fabs(len.z) ) ? *maxlen : std::fabs(len.z);
cout << "Cp are normalized." << endl;
for ( int j = 0; j < bsps.size(); ++j )
{
std::vector<Point4f>& cp = bsps[j].cp();
for ( int i = 0; i < cp.size(); ++i )
{
Point4f p1;
p1.x = (cp[i].x - center.x) / *maxlen;
p1.y = (cp[i].y - center.y) / *maxlen;
p1.z = (cp[i].z - center.z) / *maxlen;
p1.w = cp[i].w;
cp[i].set( p1 );
//cout << i << " " << p1 << endl;
}
}
}