-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathprimitive.cpp
More file actions
294 lines (248 loc) · 4.88 KB
/
Copy pathprimitive.cpp
File metadata and controls
294 lines (248 loc) · 4.88 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) 2000, 2001, 2002, 2003 by David Scherer and others.
// Copyright (c) 2003, 2004 by Jonathan Brandmeyer and others.
// See the file license.txt for complete license terms.
// See the file authors.txt for a complete list of contributors.
#include "primitive.hpp"
#include "util/errors.hpp"
#include <boost/python/import.hpp>
#include <typeinfo>
#include <cmath>
//#ifndef _MSC_VER // This was needed when we used GTK2 on Windows
#ifndef _WIN32
#include <cxxabi.h>
#endif
#include <sstream>
namespace cvisual {
tmatrix
primitive::model_world_transform(double world_scale, const vector& object_scale) const
{
// Performs scale, rotation, translation, and world scale (gcf) transforms in that order.
// ret = world_scale o translation o rotation o scale
// Note that with the default parameters, only the rotation transformation is returned! Typical
// usage should be model_world_transform( scene.gcf, my_size );
tmatrix ret;
// A unit vector along the z_axis.
vector z_axis = vector(0,0,1);
if (std::fabs(axis.dot(up) / std::sqrt( up.mag2() * axis.mag2())) > 0.98) {
// Then axis and up are in (nearly) the same direction: therefore,
// try two other possible directions for the up vector.
if (std::fabs(axis.norm().dot( vector(-1,0,0))) > 0.98)
z_axis = axis.cross( vector(0,0,1)).norm();
else
z_axis = axis.cross( vector(-1,0,0)).norm();
}
else {
z_axis = axis.cross( up).norm();
}
vector y_axis = z_axis.cross(axis).norm();
vector x_axis = axis.norm();
ret.x_column( x_axis );
ret.y_column( y_axis );
ret.z_column( z_axis );
ret.w_column( pos * world_scale );
ret.w_row();
ret.scale( object_scale * world_scale );
return ret;
}
// Oblong objects (e.g. cylinder) whose center is not at "pos" override primitive::get_center
vector
primitive::get_center() const
{
return pos;
}
using boost::python::import;
using boost::python::object;
bool startup = true;
boost::python::object trail_update;
primitive::primitive()
: axis(1,0,0), up(0,1,0), pos(0,0,0), trail_initialized(false), obj_initialized(false)
{
}
primitive::primitive( const primitive& other)
: renderable( other), axis(other.axis), up(other.up),
pos(other.pos)
{
}
primitive::~primitive()
{
}
void
primitive::rotate( double angle, const vector& _axis, const vector& origin)
{
tmatrix R = rotation( angle, _axis, origin);
vector fake_up = up;
if (!axis.cross( fake_up)) {
fake_up = vector( 1,0,0);
if (!axis.cross( fake_up))
fake_up = vector( 0,1,0);
}
{
pos = R * pos;
axis = R.times_v(axis);
up = R.times_v(fake_up);
}
}
void
primitive::set_pos( const vector& n_pos)
{
pos = n_pos;
if (trail_initialized && make_trail) {
if (obj_initialized) {
python::gil_lock gil;
trail_update(primitive_object);
}
}
}
shared_vector&
primitive::get_pos()
{
return pos;
}
void
primitive::set_x( double x)
{
pos.set_x( x);
if (trail_initialized && make_trail) {
if (obj_initialized) {
python::gil_lock gil;
trail_update(primitive_object);
}
}
}
double
primitive::get_x()
{
return pos.x;
}
void
primitive::set_y( double y)
{
pos.set_y( y);
if (trail_initialized && make_trail) {
if (obj_initialized) {
python::gil_lock gil;
trail_update(primitive_object);
}
}
}
double
primitive::get_y()
{
return pos.y;
}
void
primitive::set_z( double z)
{
pos.set_z( z);
if (trail_initialized && make_trail) {
if (obj_initialized) {
python::gil_lock gil;
trail_update(primitive_object);
}
}
}
double
primitive::get_z()
{
return pos.z;
}
void
primitive::set_axis( const vector& n_axis)
{
axis = n_axis;
}
shared_vector&
primitive::get_axis()
{
return axis;
}
void
primitive::set_up( const vector& n_up)
{
up = n_up;
}
shared_vector&
primitive::get_up()
{
return up;
}
void
primitive::set_color( const rgb& n_color)
{
color = n_color;
}
void
primitive::set_red( float r)
{
color.red = r;
}
double
primitive::get_red()
{
return color.red;
}
void
primitive::set_green( float g)
{
color.green = g;
}
double
primitive::get_green()
{
return color.green;
}
void
primitive::set_blue( float b)
{
color.blue = b;
}
double
primitive::get_blue()
{
return color.blue;
}
rgb
primitive::get_color()
{
return color;
}
void
primitive::set_opacity( float a)
{
opacity = a;
}
double
primitive::get_opacity()
{
return opacity;
}
void
primitive::set_make_trail( bool t)
{
if (t && !obj_initialized)
throw std::runtime_error( "Can't set make_trail=True unless object was created with make_trail specified");
if (startup) {
trail_update = import("vis.primitives").attr("trail_update");
startup = false;
}
make_trail = t;
trail_initialized = true;
}
bool
primitive::get_make_trail()
{
return make_trail;
}
void
primitive::set_primitive_object( boost::python::object obj)
{
primitive_object = obj;
obj_initialized = true;
}
boost::python::object
primitive::get_primitive_object()
{
return primitive_object;
}
PRIMITIVE_TYPEINFO_IMPL(primitive)
} // !namespace cvisual