SIMLIB/C++ 3.09
Loading...
Searching...
No Matches
simlib3D.cc
Go to the documentation of this file.
1/////////////////////////////////////////////////////////////////////////////
2//! \file simlib3D.cc 3D vector block extension
3//
4// Copyright (c) 1997-2004 Petr Peringer
5//
6// This library is licensed under GNU Library GPL. See the file COPYING.
7//
8
9//
10// 3D extension
11//
12// - Integrator3D
13// - blocks for + - * / operations
14//
15
16
17////////////////////////////////////////////////////////////////////////////
18// interface
19//
20
21#include "simlib.h"
22#include "simlib3D.h"
23#include "internal.h"
24#include <cmath>
25
26////////////////////////////////////////////////////////////////////////////
27// implementation
28//
29
30namespace simlib3 {
31
33
34////////////////////////////////////////////////////////////////////////////
35// non-block operations
36//
37
38double abs(const Value3D &a)
39{
40 return sqrt( a._x * a._x + a._y * a._y + a._z * a._z );
41}
42
43Value3D operator +(const Value3D& a, const Value3D &b)
44{
45 return Value3D( a._x + b._x, a._y + b._y, a._z + b._z );
46}
47
48Value3D operator -(const Value3D& a, const Value3D &b)
49{
50 return Value3D( a._x - b._x, a._y - b._y, a._z - b._z );
51}
52
54{
55 return Value3D( -a._x, -a._y, -a._z);
56}
57
58Value3D operator *(const Value3D& a, const Value3D &b)
59{
60 return Value3D( a._y * b._z - a._z * b._y,
61 a._z * b._x - a._x * b._z,
62 a._x * b._y - a._y * b._x
63 );
64}
65
66Value3D operator *(const Value3D& a, const double b)
67{
68 return Value3D( a._x * b, a._y * b, a._z * b );
69}
70
71Value3D operator *(const double a, const Value3D& b)
72{
73 return b * a;
74}
75
76double scalar_product(const Value3D& a, const Value3D &b)
77{
78 return a._x * b._x + a._y * b._y + a._z * b._z;
79}
80
81Value3D operator /(const Value3D& a, const double b)
82{
83 return Value3D( a._x / b, a._y / b, a._z / b );
84}
85
86
87////////////////////////////////////////////////////////////////////////////
88// aContiBlock3D --- constructor & destructor
89//
91 isEvaluated(false)
92{
93 Dprintf(("3Dblock-ctr"));
94}
95
97 Dprintf(("3Dblock-dtr"));
98}
99
100////////////////////////////////////////////////////////////////////////////
101// aContiBlock3D::Print --- print of 3D block output value
102//
104 Value3D a = Value();
105 a.Print();
106}
107
108////////////////////////////////////////////////////////////////////////////
109// aContiBlock::_Eval --- evaluation with algebraic loop detection ###
110//
112{
113 if(isEvaluated) // was evaluated
114 SIMLIB_error(AlgLoopDetected); // recursive call
115 isEvaluated = true; // eval-flag
116 Eval(); // evaluation of block
117 isEvaluated = false;
118}
119
120////////////////////////////////////////////////////////////////////////////
121// Parameter3D --- parameter of model
122//
126 value = x;
127 return *this;
128}
129
130
131////////////////////////////////////////////////////////////////////////////
132// aContiBlock1 --- base for blocks with one input
133//
135{
136 if(input==this)
138// LoopCheck();
139}
140
141
142////////////////////////////////////////////////////////////////////////////
143// Integrator3D::special_input::Value() --- expects 3 subsequent evaluations
144//
145// method returns scalar value for input of scalar integrator
146// three evaluations are guaranteed by Integrator3D implementation
147//
148double Integrator3D::special_input::Value() { // interface class
149 if(count == 0)
150 a = in.Value(); // get vector input
151 switch(++count) {
152 case 1: return a.x(); // first call (x)
153 case 2: return a.y(); // second call (y)
154 case 3: count=0;
155 return a.z(); // third and last call (z)
156 } // switch
157 SIMLIB_internal_error(); // *** never reached ***
158 return 0; // compiler warning elimination
159}
160
161////////////////////////////////////////////////////////////////////////////
162// Integrator3D --- constructors
163//
165 _x(in, initial_value.x()), // linking of inputs to integrators
166 _y(in, initial_value.y()),
167 _z(in, initial_value.z()),
168 in(i) {}
169
171 _x(in), _y(in), _z(in),
172 in(i) {}
173
174// --- special copy constructor
177 _x(in, initial_value.x()), // linking of inputs to integrators
178 _y(in, initial_value.y()),
179 _z(in, initial_value.z()),
180 in(i) {}
181
182// zero input for default Integrator3D constructor
183// we need it for creating arrays of integrators
184static Constant3D Zero3D(0,0,0);
185
187 _x(in), _y(in), _z(in),
188 in(Zero3D) {}
189
190////////////////////////////////////////////////////////////////////////////
191// Integrator3D output method
192//
194{
195 return Value3D(_x.Value(), _y.Value(), _z.Value());
196}
197
198////////////////////////////////////////////////////////////////////////////
199// operator =
200//
202{
203 _x=a.x();
204 _y=a.y();
205 _z=a.z();
206 return *this;
207}
208
210{
211 Value3D a = i.Value();
212 _x=a.x();
213 _y=a.y();
214 _z=a.z();
215 return *this;
216}
217
218////////////////////////////////////////////////////////////////////////////
219// aContiBlock2 --- base for 2 input blocks
220//
222 : input1(i1), input2(i2)
223{
224 if(input1==this || input2==this)
226// LoopCheck();
227}
228
230 : aContiBlock3D2(i1,i2), input3(i3)
231{
232 if(input3==this )
234}
235
236
237////////////////////////////////////////////////////////////////////////////
238// Add --- sum of two inputs
239//
240class _Add3D : public aContiBlock3D2 {
241 virtual void _Eval() override {}
242public:
244 Dprintf(("ctr: _Add3D[%p](in1,in2)", this));
245 }
246 virtual Value3D Value() override {
247 Value3D a = Input1Value();
248 Value3D b = Input2Value();
249 return a + b;
250 }
251};
252
253
254////////////////////////////////////////////////////////////////////////////
255// Sub --- subtract two inputs
256//
257class _Sub3D : public aContiBlock3D2 {
258 virtual void _Eval() override {}
259public:
261 Dprintf(("ctr: _Sub3D[%p](in1,in2)", this));
262 }
263 virtual Value3D Value() override {
264 Value3D a = Input1Value();
265 Value3D b = Input2Value();
266 return a - b;
267 }
268};
269
270
271////////////////////////////////////////////////////////////////////////////
272// Mul --- vector multiplier
273//
274class _Mul3D : public aContiBlock3D2 {
275 virtual void _Eval() override {}
276public:
278 Dprintf(("ctr: _Mul3D[%p](in1,in2)", this));
279 }
280 virtual Value3D Value() override {
281 Value3D a = Input1Value();
282 Value3D b = Input2Value();
283 return a * b;
284 }
285};
286
287////////////////////////////////////////////////////////////////////////////
288// Mul --- multiplier vector * scalar
289//
290class _Mul3D1D : public aContiBlock3D1 {
292 virtual void _Eval() override {}
293public:
295 Dprintf(("ctr: _Mul3D1D[%p](in1,in2)", this));
296 }
297 virtual Value3D Value() override {
298 Value3D a = InputValue();
299 double b = _b.Value();
300 return a * b;
301 }
302};
303
304
305////////////////////////////////////////////////////////////////////////////
306// Div --- divider vector / scalar
307//
308class _Div3D : public aContiBlock3D1 {
310 virtual void _Eval() override {}
311public:
313 Dprintf(("ctr: _Div3D[%p](in1_3D,in2)", this));
314 }
315 virtual Value3D Value() override {
316 Value3D a = InputValue();
317 double b = _b.Value();
318 return a / b;
319 }
320};
321
322////////////////////////////////////////////////////////////////////////////
323// binary operators ...
324//
325// wrapper operator functions for block expressions
326//
327
328Input3D operator + (Input3D a, Input3D b) { return new _Add3D(a,b); }
329Input3D operator - (Input3D a, Input3D b) { return new _Sub3D(a,b); }
330Input3D operator * (Input3D a, Input3D b) { return new _Mul3D(a,b); }
331Input3D operator * (Input3D a, Input b) { return new _Mul3D1D(a,b); }
332Input3D operator * (Input a, Input3D b) { return new _Mul3D1D(b,a); }
333Input3D operator / (Input3D a, Input b) { return new _Div3D(a,b); }
334
335////////////////////////////////////////////////////////////////////////////
336// UMinus --- unary minus
337//
339 virtual void _Eval() override {}
340public:
342 Dprintf(("ctr: _UMinus3D[%p](in)", this));
343 }
344 virtual Value3D Value() override {
345 Value3D a = InputValue();
346 return -a;
347 }
348};
349
350////////////////////////////////////////////////////////////////////////////
351// unary operators ...
352//
354
355////////////////////////////////////////////////////////////////////////////
356// functions for block expressions
357//
358
359////////////////////////////////////////////////////////////////////////////
360// _Abs3D --- absolute value of vector x
361//
362class _Abs3D: public aContiBlock {
363 virtual void _Eval() override {}
365public:
366 _Abs3D(Input3D a): in(a) {}
367 virtual double Value() override {
368 Value3D a = in.Value();
369 return abs(a);
370 }
371};
372
373////////////////////////////////////////////////////////////////////////////
374// _Norm3D --- unit vector = vector x/Abs(x)
375//
376class _Norm3D: public aContiBlock3D1 {
377 virtual void _Eval() override {}
378public:
380 virtual Value3D Value() override {
381 Value3D a = InputValue();
382 return a/abs(a);
383 }
384};
385
386////////////////////////////////////////////////////////////////////////////
387// _ScalarMul3D --- scalar multiply
388//
390 virtual void _Eval() override {}
393public:
395 virtual double Value() override {
396 Value3D a = input1.Value();
397 Value3D b = input2.Value();
398 return scalar_product(a,b);
399 }
400};
401
402////////////////////////////////////////////////////////////////////////////
403// wrapper functions for block expressions
404//
405
406Input Abs(Input3D x) { return new _Abs3D(x); } // absolute value of vector x
407Input3D UnitVector(Input3D x) { return new _Norm3D(x); }
409
410
411////////////////////////////////////////////////////////////////////////////
412// _XYZpart --- vector part
413//
414class _XYZpart: public aContiBlock {
415 virtual void _Eval() override {}
417 public:
418 enum WhichPart { x,y,z }; // part identification
420 virtual double Value() override {
421 Value3D a = input.Value();
422 switch(which) {
423 case x: return a.x();
424 case y: return a.y();
425 case z: return a.z();
426 }
427 SIMLIB_internal_error(); // never reached
428 return 0; // compiler warning elimination
429 }
430 private:
432};
433
434////////////////////////////////////////////////////////////////////////////
435// get x,y,z part of vector block
436//
437// wrapper functions for block expressions
438//
439
440Input Xpart(Input3D a) { return new _XYZpart(a, _XYZpart::x); }
441Input Ypart(Input3D a) { return new _XYZpart(a, _XYZpart::y); }
442Input Zpart(Input3D a) { return new _XYZpart(a, _XYZpart::z); }
443
444
445} // end
446
3D vector value that can't be changed
Definition: simlib3D.h:78
Continuous block connection (transparent reference)
Definition: simlib3D.h:110
Value3D Value()
Definition: simlib3D.h:118
continuous block connection (transparent reference) wrapper for pointer to objects of aContiBlock der...
Definition: simlib.h:895
double Value() const
get target block value
Definition: simlib.h:931
double Value() override
get block output value this method should be defined in classes derived from aContiBlock
Definition: simlib3D.cc:148
3D vector integrator
Definition: simlib3D.h:180
virtual Value3D Value() override
Definition: simlib3D.cc:193
simlib3::Integrator3D::special_input in
Integrator3D & operator=(const Value3D &a)
Definition: simlib3D.cc:201
double Value() override
the state of integrator
Definition: intg.cc:249
Special variable (can't be changed at simulation time)
Definition: simlib3D.h:101
Parameter3D & operator=(const Value3D &x)
Definition: simlib3D.cc:123
3D vector value
Definition: simlib3D.h:31
double y() const
Definition: simlib3D.h:36
void Print()
Definition: simlib3D.h:43
double x() const
Definition: simlib3D.h:35
double z() const
Definition: simlib3D.h:37
_Abs3D(Input3D a)
Definition: simlib3D.cc:366
virtual void _Eval() override
evaluate block (with loop detection)
Definition: simlib3D.cc:363
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock
Definition: simlib3D.cc:367
virtual void _Eval() override
Definition: simlib3D.cc:241
_Add3D(Input3D a, Input3D b)
Definition: simlib3D.cc:243
virtual Value3D Value() override
Definition: simlib3D.cc:246
virtual Value3D Value() override
Definition: simlib3D.cc:315
virtual void _Eval() override
Definition: simlib3D.cc:310
_Div3D(Input3D a, Input b)
Definition: simlib3D.cc:312
_Mul3D1D(Input3D a, Input b)
Definition: simlib3D.cc:294
virtual Value3D Value() override
Definition: simlib3D.cc:297
virtual void _Eval() override
Definition: simlib3D.cc:292
virtual void _Eval() override
Definition: simlib3D.cc:275
_Mul3D(Input3D a, Input3D b)
Definition: simlib3D.cc:277
virtual Value3D Value() override
Definition: simlib3D.cc:280
virtual void _Eval() override
Definition: simlib3D.cc:377
virtual Value3D Value() override
Definition: simlib3D.cc:380
_Norm3D(Input3D a)
Definition: simlib3D.cc:379
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock
Definition: simlib3D.cc:395
_ScalarProduct3D(Input3D a, Input3D b)
Definition: simlib3D.cc:394
virtual void _Eval() override
evaluate block (with loop detection)
Definition: simlib3D.cc:390
virtual void _Eval() override
Definition: simlib3D.cc:258
_Sub3D(Input3D a, Input3D b)
Definition: simlib3D.cc:260
virtual Value3D Value() override
Definition: simlib3D.cc:263
virtual Value3D Value() override
Definition: simlib3D.cc:344
_UMinus3D(Input3D a)
Definition: simlib3D.cc:341
virtual void _Eval() override
Definition: simlib3D.cc:339
virtual void _Eval() override
evaluate block (with loop detection)
Definition: simlib3D.cc:415
WhichPart which
Definition: simlib3D.cc:431
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock
Definition: simlib3D.cc:420
_XYZpart(Input3D a, WhichPart w)
Definition: simlib3D.cc:419
continuous 3D block with single input
Definition: simlib3D.h:125
aContiBlock3D1(Input3D i)
Definition: simlib3D.cc:134
continuous block vith 2 inputs and alg.
Definition: simlib3D.h:144
aContiBlock3D2(Input3D i1, Input3D i2)
Definition: simlib3D.cc:221
aContiBlock3D3(Input3D i1, Input3D i2, Input3D i3)
Definition: simlib3D.cc:229
Abstract 3D block with single 3D output.
Definition: simlib3D.h:62
virtual Value3D Value()=0
virtual void Eval()
Definition: simlib3D.h:66
virtual void _Eval()
Definition: simlib3D.cc:111
abstract base for continuous blocks with single output suitable for expression-tree building and eval...
Definition: simlib.h:832
@ ParameterChangeErr
Definition: errors.h:89
@ AlgLoopDetected
Definition: errors.h:65
Internal header file for SIMLIB/C++.
#define Dprintf(f)
Definition: internal.h:100
#define SIMLIB_internal_error()
Definition: internal.h:167
Implementation of class CalendarList interface is static - using global functions in SQS namespace.
Definition: algloop.cc:32
Input operator-(Input a, Input b)
block operator -
Definition: continuous.cc:226
void SIMLIB_error(const enum _ErrEnum N)
print error message and abort program
Definition: error.cc:38
Input operator+(Input a, Input b)
block operator +
Definition: continuous.cc:224
@ SIMULATION
Definition: internal.h:79
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
Input Abs(Input x)
Definition: fun.cc:94
double abs(const Value2D &a)
Definition: simlib2D.cc:38
Input Ypart(Input2D a)
get y part of (x,y) vector value
Definition: simlib2D.cc:421
Input Xpart(Input2D a)
get x part of (x,y) vector value
Definition: simlib2D.cc:420
Input operator*(Input a, Input b)
block operator *
Definition: continuous.cc:228
double scalar_product(const Value2D &a, const Value2D &b)
Definition: simlib2D.cc:78
Input ScalarProduct(Input2D x, Input2D y)
dot product: xvec . yvec
Definition: simlib2D.cc:389
Input Zpart(Input3D a)
get z part of (x,y,z) vector value
Definition: simlib3D.cc:442
static Constant3D Zero3D(0, 0, 0)
SIMLIB_Phase_t SIMLIB_Phase
Definition: run.cc:57
Input operator/(Input a, Input b)
block operator /
Definition: continuous.cc:230
Input2D UnitVector(Input2D x)
make unit vector from input (Abs(output_vec)==1)
Definition: simlib2D.cc:388
3D vector block extension
Main SIMLIB/C++ interface.