Debian Codegraph @

07p/plaut04/src/polygon3d.c++

raw
1 #include <Inventor/SbLinear.h>
2 #include "polygon3d.h"
3
4 void Polygon3D::init(float r, int nv)
5 {
6 n_points = nv;
7 radius = r;
8 normal.setValue(0.0, 0.0, 1.0);
9 center.setValue(0.0, 0.0, 0.0);
10
11 points = new SbVec3f[n_points];
12 float dfi = (M_PI * 2) / n_points;
13 for (int i=0; i<n_points; i++)
14 points[i].setValue(radius*cos(dfi*i), radius*sin(dfi*i), 0.0);
15 }
16
17
18 Polygon3D::Polygon3D(float r, int nv)
19 {
20 init(r, nv);
21 }
22
23
24 Polygon3D::Polygon3D(const SbVec3f &c, const SbVec3f &n, float r, int nv)
25 {
26 init(r, nv);
27 rotate(n);
28 translate(c);
29 }
30
31
32 Polygon3D::~Polygon3D()
33 {
34 if (points != NULL)
35 delete [] points;
36 }
37
38
39 void Polygon3D::rotate(const SbVec3f &new_normal)
40 {
41 SbRotation rot = SbRotation(normal, new_normal);
42 for (int i=0; i<n_points; i++)
43 {
44 rot.multVec(points[i]-center, points[i]);
45 points[i] += center;
46 }
47 normal = new_normal;
48
49 #ifdef _normalize_normals_
50 normal.normalize();
51 #endif
52 }
53
54
55 void Polygon3D::translate(const SbVec3f &t)
56 {
57 for (int i=0; i<n_points; i++)
58 points[i] += t;
59 center += t;
60 }
61
62
63 void Polygon3D::moveTo(const SbVec3f &c)
64 {
65 for (int i=0; i<n_points; i++)
66 points[i] += c - center;
67 center = c;
68 }
69
70
71 void Polygon3D::scale(float s)
72 {
73 for (int i=0; i<n_points; i++)
74 points[i] = (points[i]-center)*s + center;
75 }
76
77
78 void Polygon3D::project(const SbPlane &p, float pj[][3])
79 {
80 SbLine line;
81 SbVec3f pt;
82
83 for (int i=0; i<n_points; i++)
84 {
85 line.setValue(points[i], points[i] + normal);
86 p.intersect(line, pt);
87 pt.getValue(pj[i][0], pj[i][1], pj[i][2]);
88 }
89 }
90
91
92 void Polygon3D::project1(const SbPlane &p, float pj[][3])
93 {
94 SbLine line;
95 SbVec3f pt;
96
97 for (int i=0; i<n_points; i++)
98 {
99 line.setValue(points[i], points[i] + p.getNormal());
100 p.intersect(line, pt);
101 pt.getValue(pj[i][0], pj[i][1], pj[i][2]);
102 }
103 }
104
105
106 void Polygon3D::rotate(const int n)
107 {
108 if (n == 0) return;
109 }
110
111
112 void Polygon3D::print()
113 {
114 for (int i=0; i<n_points; i++)
115 printf("%12f %12f %12f\n", points[i][0], points[i][1], points[i][2]);
116 }