Debian Codegraph @

apps/demos/noodle/LineManipHilight.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 //
38 // Stuff to hilight parts of the line-set manipulator
39 //
40
41 #include <cstdio>
42 #include <cstring>
43 #include <cmath>
44 #include <assert.h>
45
46 #include <Inventor/SoDB.h>
47 #include <Inventor/nodes/SoPickStyle.h>
48 #include <Inventor/nodes/SoTransform.h>
49 #include <Inventor/nodes/SoMaterial.h>
50 #include <Inventor/nodes/SoSwitch.h>
51 #include <Inventor/nodes/SoCoordinate3.h>
52 #include <Inventor/nodes/SoLineSet.h>
53 #include <Inventor/SoInput.h>
54
55 #include "LineManip.h"
56
57 void
58 LineManip2::setHilightSize(float size)
59 {
60 hilightSize = size;
61 //
62 // This should really be figured out during event handling to give the
63 // hilights a uniform size in screen space (say, 5 or 10 pixels).
64 //
65 hilightTransform->scaleFactor = SbVec3f(hilightSize, hilightSize,
66 hilightSize);
67 }
68
69 void
70 LineManip2::initHilightStuff()
71 {
72 SoSeparator *sep = new SoSeparator;
73 addChild(sep);
74
75 SoPickStyle *ps = new SoPickStyle;
76 sep->addChild(ps);
77 ps->style = SoPickStyle::UNPICKABLE;
78
79 hilightMaterial = new SoMaterial;
80 sep->addChild(hilightMaterial);
81
82 hilightSwitch = new SoSwitch;
83 sep->addChild(hilightSwitch);
84 hilightSwitch->whichChild = SO_SWITCH_NONE;
85
86 hilightTransform = new SoTransform;
87 //
88 // This should really be figured out during event handling to give the
89 // hilights a uniform size in screen space (say, 5 or 10 pixels).
90 //
91 hilightTransform->scaleFactor = SbVec3f(hilightSize, hilightSize,
92 hilightSize);
93
94 { // VERTEX HILIGHT MUST BE CHILD 0
95 // The vertex hilight is a little cross-hair:
96 //
97 static const float vhCoords[8][3] = {
98 {.25, 0, 0}, {1, 0, 0}, {0, .25, 0}, {0, 1, 0},
99 {-.25, 0, 0}, {-1, 0, 0}, {0, -.25, 0}, {0, -1, 0},
100 };
101 static int32_t lsNV[4] = { 2, 2, 2, 2 };
102
103 SoGroup *g = new SoGroup;
104 hilightSwitch->addChild(g);
105 g->addChild(hilightTransform);
106 SoCoordinate3 *c = new SoCoordinate3;
107 g->addChild(c);
108 c->point.setValues(0, 8, vhCoords);
109 SoLineSet *ls = new SoLineSet;
110 g->addChild(ls);
111 ls->numVertices.setValues(0, 4, lsNV);
112 }
113 { // LINE HILIGHT MUST BE CHILD 1
114 // The line hilight is a line (or lines) to the new vertex and
115 // an octagon surrounding the new vertex.
116 //
117 SoGroup *g = new SoGroup;
118 hilightSwitch->addChild(g);
119
120 // First, lines from previous to next vertex
121 hilightCoord = new SoCoordinate3;
122 g->addChild(hilightCoord);
123 SoLineSet *ls1 = new SoLineSet;
124 g->addChild(ls1);
125 ls1->numVertices.setValue(3);
126
127 // Second: octagon:
128 static const float lhCoords[9][3] = {
129 {1, 0, 0}, {M_SQRT1_2, M_SQRT1_2, 0},
130 {0, 1, 0}, {-M_SQRT1_2, M_SQRT1_2, 0},
131 {-1, 0, 0}, {-M_SQRT1_2, -M_SQRT1_2, 0},
132 {0, -1, 0}, {M_SQRT1_2, -M_SQRT1_2, 0},
133 {1, 0, 0}
134 };
135 g->addChild(hilightTransform);
136 SoCoordinate3 *c = new SoCoordinate3;
137 g->addChild(c);
138 c->point.setValues(0, 9, lhCoords);
139 SoLineSet *ls2 = new SoLineSet;
140 g->addChild(ls2);
141 ls2->numVertices.setValue(9);
142 }
143 }
144
145 void
146 LineManip2::hilightVertex(const SbVec3f &position, const SbColor &color)
147 {
148 // Set switch to vertex (if necessary)
149 if (hilightSwitch->whichChild.getValue() != 0)
150 {
151 hilightSwitch->whichChild.setValue(0);
152 }
153 // Set transformation to given position (if necessary)
154 if (hilightTransform->translation.getValue() != position)
155 {
156 hilightTransform->translation.setValue(position);
157 }
158 // And set color, if necessary.
159 if (hilightMaterial->diffuseColor[0] != color)
160 {
161 hilightMaterial->diffuseColor.setValue(color);
162 }
163 }
164
165 void
166 LineManip2::hilightLine(int whichLine, const SbVec3f &position,
167 const SbColor &color)
168 {
169 if (coord == NULL)
170 return;
171
172 // Draw the line hilight
173 if (hilightSwitch->whichChild.getValue() != 1)
174 {
175 hilightSwitch->whichChild.setValue(1);
176 }
177
178 // Set transformation to given position
179 hilightTransform->translation.setValue(position);
180
181 // And set color, if necessary.
182 if (hilightMaterial->diffuseColor[0] != color)
183 {
184 hilightMaterial->diffuseColor.setValue(color);
185 }
186
187 int n = coord->point.getNum();
188 // And set coordinates of the extra line segments:
189 if (whichLine >= 0 && whichLine < n)
190 {
191 hilightCoord->point.set1Value(0, coord->point[whichLine]);
192 }
193 else
194 {
195 hilightCoord->point.set1Value(0, position);
196 }
197 if (whichLine+1 < coord->point.getNum() && whichLine+1 >= 0)
198 {
199 hilightCoord->point.set1Value(2, coord->point[whichLine+1]);
200 }
201 else
202 {
203 hilightCoord->point.set1Value(2, position);
204 }
205 hilightCoord->point.set1Value(1, position);
206 }
207
208 void
209 LineManip2::removeHilights()
210 {
211 if (hilightSwitch->whichChild.getValue() != SO_SWITCH_NONE)
212 {
213 hilightSwitch->whichChild.setValue(SO_SWITCH_NONE);
214 }
215 }