forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opennurbs_detail.cpp
241 lines (202 loc) · 6.16 KB
/
opennurbs_detail.cpp
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
/* $NoKeywords: $ */
/*
//
// Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
*/
#include "opennurbs.h"
#if !defined(ON_COMPILING_OPENNURBS)
// This check is included in all opennurbs source .c and .cpp files to insure
// ON_COMPILING_OPENNURBS is defined when opennurbs source is compiled.
// When opennurbs source is being compiled, ON_COMPILING_OPENNURBS is defined
// and the opennurbs .h files alter what is declared and how it is declared.
#error ON_COMPILING_OPENNURBS must be defined when compiling opennurbs
#endif
ON_OBJECT_IMPLEMENT(ON_DetailView,ON_Geometry,"C8C66EFA-B3CB-4e00-9440-2AD66203379E");
ON_DetailView::ON_DetailView()
{
m_page_per_model_ratio = 0.0;
}
ON_DetailView::~ON_DetailView()
{
}
void ON_DetailView::MemoryRelocate()
{
m_boundary.MemoryRelocate();
}
bool ON_DetailView::IsValid( ON_TextLog* text_log ) const
{
// Don't bother checking m_view - during runtime it's
// not filled in. It is only used for IO. See
// CRhDetailViewObject::PrepareToWrite() for details.
return m_boundary.IsValid(text_log);
}
void ON_DetailView::Dump( ON_TextLog& text_log ) const
{
m_view.Dump(text_log);
m_boundary.Dump(text_log);
}
unsigned int ON_DetailView::SizeOf() const
{
unsigned int sz = ON_Geometry::SizeOf();
sz += sizeof(*this) - sizeof(ON_Geometry);
sz += m_boundary.SizeOf();
return sz;
}
bool ON_DetailView::Write( ON_BinaryArchive& archive ) const
{
bool rc = archive.BeginWrite3dmChunk( TCODE_ANONYMOUS_CHUNK, 1, 1 );
if (!rc)
return false;
for(;;)
{
// m_view is wrapped in a subchunk so ON_3dmView can be expanded
// without breaking the file format.
rc = archive.BeginWrite3dmChunk( TCODE_ANONYMOUS_CHUNK, 1, 0 );
if (rc)
{
rc = m_view.Write(archive);
if (!archive.EndWrite3dmChunk())
rc = false;
}
if(!rc)
break;
// m_boundary is wrapped in a subchunk so ON_NurbsCurve can be expanded
// without breaking the file format.
rc = archive.BeginWrite3dmChunk( TCODE_ANONYMOUS_CHUNK, 1, 0 );
if (rc)
{
rc = m_boundary.Write(archive)?true:false;
if (!archive.EndWrite3dmChunk())
rc = false;
}
if(!rc)
break;
// 28 Feb 2006 1.1 fields added
rc = archive.WriteDouble(m_page_per_model_ratio);
if ( !rc )
break;
break;
}
if ( !archive.EndWrite3dmChunk() )
rc = false;
return rc;
}
bool ON_DetailView::Read(ON_BinaryArchive& archive)
{
m_page_per_model_ratio = 0.0;
m_view.Default();
m_boundary.Destroy();
int major_version = 0;
int minor_version = 0;
bool rc = archive.BeginRead3dmChunk( TCODE_ANONYMOUS_CHUNK, &major_version, &minor_version );
if (!rc)
return false;
for(;;)
{
rc = (1 == major_version );
if (!rc) break;
// m_view is wrapped in a subchunk so ON_3dmView can be expanded
// without breaking the file format.
int mj = 0, mn = 0;
rc = archive.BeginRead3dmChunk( TCODE_ANONYMOUS_CHUNK, &mj, &mn );
if (rc)
{
rc = m_view.Read(archive);
if (!archive.EndRead3dmChunk())
rc = false;
}
if (!rc) break;
// m_boundary is wrapped in a subchunk so ON_NurbsCurve can be expanded
// without breaking the file format.
mj = mn = 0;
rc = archive.BeginRead3dmChunk( TCODE_ANONYMOUS_CHUNK, &mj, &mn );
if (rc)
{
rc = m_boundary.Read(archive)?true:false;
if (!archive.EndRead3dmChunk())
rc = false;
}
if (!rc) break;
if ( minor_version >= 1 )
{
rc = archive.ReadDouble(&m_page_per_model_ratio);
}
break;
}
if ( !archive.EndRead3dmChunk() )
rc = false;
return rc;
}
ON::object_type ON_DetailView::ObjectType() const
{
return ON::detail_object;
}
int ON_DetailView::Dimension() const
{
return m_boundary.Dimension();
}
bool ON_DetailView::GetBBox(
double* boxmin,
double* boxmax,
bool bGrowBox
) const
{
return m_boundary.GetBBox(boxmin,boxmax,bGrowBox);
}
bool ON_DetailView::GetTightBoundingBox(
ON_BoundingBox& tight_bbox, bool bGrowBox, const ON_Xform* xform
) const
{
return m_boundary.GetTightBoundingBox(tight_bbox,bGrowBox,xform);
}
bool ON_DetailView::Transform( const ON_Xform& xform )
{
return m_boundary.Transform(xform);
}
bool ON_DetailView::UpdateFrustum(
ON::LengthUnitSystem model_units,
ON::LengthUnitSystem paper_units
)
{
if (!m_view.m_vp.IsParallelProjection())
return false;
if (!(m_page_per_model_ratio > 0.0))
return false;
ON_BoundingBox bbox = BoundingBox();
double port_width = bbox.m_max.x - bbox.m_min.x;
double port_height = bbox.m_max.y - bbox.m_min.y;
if (!(port_height > 0.0) || !(port_width > 0.0))
return false;
double detail_width_on_paper = bbox.m_max.x - bbox.m_min.x;
double detail_width_on_paper_mm = detail_width_on_paper * ON::UnitScale(paper_units, ON::LengthUnitSystem::Millimeters);
if (!(detail_width_on_paper_mm > 0.0))
return false;
double frustum_width_mm = detail_width_on_paper_mm / m_page_per_model_ratio;
double frustum_width = frustum_width_mm * ON::UnitScale(ON_UnitSystem(ON::LengthUnitSystem::Millimeters), model_units);
double aspect = fabs(port_width / port_height);
if (!(aspect > 0.0))
return false;
double frustum_height = frustum_width / aspect;
if (!(frustum_height > 0.0))
return false;
double fr_left, fr_right, fr_top, fr_bottom, fr_near, fr_far;
if (m_view.m_vp.GetFrustum(&fr_left, &fr_right, &fr_bottom, &fr_top, &fr_near, &fr_far))
{
fr_left = (fr_left + fr_right) / 2.0 - frustum_width / 2.0;
fr_right = fr_left + frustum_width;
fr_bottom = (fr_bottom + fr_top) / 2.0 - frustum_height / 2.0;
fr_top = fr_bottom + frustum_height;
return m_view.m_vp.SetFrustum(fr_left, fr_right, fr_bottom, fr_top, fr_near, fr_far);
}
return false;
}