1 // Copyright (C) 2006 Erik Dahlberg
3 // This file is part of LSystem3D.
5 // LSystem3D is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // LSystem3D 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
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with LSystem3D; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "coordinate.h"
37 * @param model create this model
39 Turtle::Turtle(Model
*model
)
43 // initial position and orientation
64 void Turtle::turnLeft()
66 // rotate around "up vector"
67 _heading
.rotate(-_angle
, _up
);
68 _left
.rotate(-_angle
, _up
);
76 void Turtle::turnRight()
78 // rotate around "up vector"
79 _heading
.rotate(_angle
, _up
);
80 _left
.rotate(_angle
, _up
);
88 void Turtle::pitchDown()
90 // rotate around "left vector"
91 _heading
.rotate(-_angle
, _left
);
92 _up
.rotate(-_angle
, _left
);
100 void Turtle::pitchUp()
102 // rotate around "left vector"
103 _heading
.rotate(_angle
, _left
);
104 _up
.rotate(_angle
, _left
);
112 void Turtle::rollLeft()
114 // rotate around "heading vector"
115 _left
.rotate(_angle
, _heading
);
116 _up
.rotate(_angle
, _heading
);
124 void Turtle::rollRight()
126 // rotate around "heading vector"
127 _left
.rotate(-_angle
, _heading
);
128 _up
.rotate(-_angle
, _heading
);
136 void Turtle::turnAround()
138 // rotate 180 degrees around "up vector"
139 _heading
.rotate(M_PIl
, _up
);
140 _left
.rotate(M_PIl
, _up
);
151 double startX
= _position
.getX();
152 double startY
= _position
.getY();
153 double startZ
= _position
.getZ();
156 double endX
= startX
+ _heading
.getX();
157 double endY
= startY
+ _heading
.getY();
158 double endZ
= startZ
+ _heading
.getZ();
161 // move turtle to new position
162 _position
.setXYZ(endX
, endY
, endZ
);
164 // connect start and end point
165 _model
->segment(startX
, startY
, startZ
,
172 * Walk forward, creating a filled surface
173 * TODO: merge with walk() ?
175 void Turtle::fillWalk()
178 double destinationX
= _position
.getX() + _heading
.getX();
179 double destinationY
= _position
.getY() + _heading
.getY();
180 double destinationZ
= _position
.getZ() + _heading
.getZ();
182 _model
->normal(_up
.getX(), _up
.getY(), _up
.getZ());
184 // move turtle to new position
185 _position
.setXYZ(destinationX
, destinationY
, destinationZ
);
187 // part of a filled polygon
188 _model
->vertex(destinationX
, destinationY
, destinationZ
);
194 * Save current state to stack
199 _positionStack
.push(_position
);
202 _headingStack
.push(_heading
);
203 _leftStack
.push(_left
);
207 double diameter
= _model
->getDiameter();
208 _diameterStack
.push(diameter
);
211 int colorIndex
= _model
->getColorIndex();
212 _colorIndexStack
.push(colorIndex
);
218 * Restore old state from stack
223 _position
= _positionStack
.top(); _positionStack
.pop();
226 _heading
= _headingStack
.top(); _headingStack
.pop();
227 _left
= _leftStack
.top(); _leftStack
.pop();
228 _up
= _upStack
.top(); _upStack
.pop();
231 double diameter
= _diameterStack
.top(); _diameterStack
.pop();
232 _model
->setDiameter(diameter
);
235 int colorIndex
= _colorIndexStack
.top(); _colorIndexStack
.pop();
236 _model
->setColorIndex(colorIndex
);
242 * Reset to default state
246 // put turtle at (0,0,0), head upwards, back towards camera
248 _position
.setXYZ(0.0, 0.0, 0.0);
250 _heading
.setXYZ(0.0, 1.0, 0.0);
251 _left
.setXYZ(1.0, 0.0, 0.0);
252 _up
.setXYZ(0.0, 0.0, 1.0);
256 _positionStack
= stack
<Coordinate
>();
257 _headingStack
= stack
<Vector
>();
258 _leftStack
= stack
<Vector
>();
259 _upStack
= stack
<Vector
>();
260 _diameterStack
= stack
<double>();
261 _colorIndexStack
= stack
<int>();
267 * Set turn/pitch/roll angle
268 * @param angle the angle, in radians
270 void Turtle::setAngle(double radians
)
278 * Get turn/pitch/roll angle
279 * @return the angle, in radians
281 double Turtle::getAngle()