root / trunk / org.gvsig.dwg / org.gvsig.dwg.lib / src / main / java / org / gvsig / dwg / lib / util / AcadExtrusionCalculator.java @ 5
History | View | Annotate | Download (5.72 KB)
1 |
/* jdwglib. Java Library for reading Dwg files.
|
---|---|
2 |
*
|
3 |
* Author: Jose Morell Rama (jose.morell@gmail.com).
|
4 |
* Port from the Pythoncad Dwg library by Art Haas.
|
5 |
*
|
6 |
* Copyright (C) 2005 Jose Morell, IVER TI S.A. and Generalitat Valenciana
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Jose Morell (jose.morell@gmail.com)
|
25 |
*
|
26 |
* or
|
27 |
*
|
28 |
* IVER TI S.A.
|
29 |
* C/Salamanca, 50
|
30 |
* 46005 Valencia
|
31 |
* Spain
|
32 |
* +34 963163400
|
33 |
* dac@iver.es
|
34 |
*/
|
35 |
package org.gvsig.dwg.lib.util; |
36 |
|
37 |
import java.lang.Math; |
38 |
|
39 |
/**
|
40 |
* This class allows to apply the extrusion transformation of Autocad given by an array
|
41 |
* of doubles to a point given by an array of doubles too.
|
42 |
*
|
43 |
* @author azabala
|
44 |
*/
|
45 |
/*
|
46 |
* The maths behind all this stuff, and the reasons to do this could be complex.
|
47 |
*
|
48 |
* Some links:
|
49 |
* http://www.autodesk.com/techpubs/autocad/acadr14/dxf/object_coordinate_systems_40ocs41_al_u05_c.htm
|
50 |
* http://www.autodesk.com/techpubs/autocad/acadr14/dxf/arbitrary_axis_algorithm_al_u05_c.htm
|
51 |
* http://personales.unican.es/togoresr/Sco-en.html
|
52 |
*
|
53 |
* In ACAD, 3D entities has their coordinates in World Coordinate System (WCS: utm, etc.)
|
54 |
* but 2D entities has their coordinates in an Object Coordinate System (OCS).
|
55 |
*
|
56 |
* To work with the coordinates saved in the file, we must to transform them (extrude) from OCS
|
57 |
* to WCS
|
58 |
*
|
59 |
|
60 |
* */
|
61 |
public class AcadExtrusionCalculator { |
62 |
|
63 |
public static Matrix4D computeTransform(double[] extrusion, double[] coord) |
64 |
{ |
65 |
|
66 |
Vector3D Ax = null;
|
67 |
if(extrusion[0] == 0d && extrusion[1] == 0d && extrusion[2] > 0d && coord[2] == 0d) |
68 |
return null; |
69 |
if((extrusion[0] >= 0f ? extrusion[0]:-extrusion[0]) < 0.015625F |
70 |
&& |
71 |
(extrusion[1] >= 0f ? extrusion[1]:-extrusion[1]) < 0.015625F){ |
72 |
|
73 |
Ax = new Vector3D(extrusion[2], 0f, -extrusion[0]); |
74 |
}else{
|
75 |
Ax = new Vector3D(-extrusion[1],extrusion[0], 0f); |
76 |
} |
77 |
double len = Ax.length();
|
78 |
Ax.scale(1d / len);
|
79 |
Vector3D upward = new Vector3D(extrusion[0], extrusion[1], extrusion[2]); |
80 |
Vector3D Ay = upward.cross(Ax); |
81 |
|
82 |
Matrix4D aaa = new Matrix4D(Ax.x, Ay.x, upward.x, coord[2] * upward.x, |
83 |
Ax.y, Ay.y, upward.y, coord[2] * upward.y,
|
84 |
Ax.z, Ay.z, upward.z, coord[2] * upward.z,
|
85 |
0.0F, 0.0F, 0.0F, 1.0F); |
86 |
|
87 |
//Creo que la transformacion comentada es para pasar de WCS a OCS, y no al reves
|
88 |
|
89 |
/*
|
90 |
Matrix4D aaa = new Matrix4D(Ax.x, Ax.y, Ax.z, 0f,
|
91 |
Ay.x, Ay.y, Ay.z, 0f,
|
92 |
upward.x, upward.y, upward.z, 0f,
|
93 |
coord[2] * upward.x, coord[2] * upward.y, coord[2] * upward.z, 1f);
|
94 |
|
95 |
*/
|
96 |
return aaa;
|
97 |
} |
98 |
|
99 |
public static double[] extrude2(double[] coord_in, double[] xtru){ |
100 |
|
101 |
//antes de cambiar el plano, aplicamos una extrusion con valor
|
102 |
//la altura del punto
|
103 |
// coord_in[0] += (xtru[0] * coord_in[2]);
|
104 |
// coord_in[1] += (xtru[1] * coord_in[2]);
|
105 |
// coord_in[2] += (xtru[2] * coord_in[2]);
|
106 |
|
107 |
|
108 |
|
109 |
Matrix4D transform = computeTransform(xtru, coord_in); |
110 |
if(transform == null)//xtru es el eje Z |
111 |
return coord_in;
|
112 |
Point3D p = new Point3D(coord_in[0], coord_in[1], coord_in[2]); |
113 |
transform.transform(p); |
114 |
return new double[]{p.x, p.y, p.z}; |
115 |
|
116 |
} |
117 |
|
118 |
|
119 |
|
120 |
/*
|
121 |
|
122 |
llamamos a finalConv, y este llama a extrude internamente
|
123 |
|
124 |
static DrawAble finalConv(DrawAble dl, DxfEntity entity, boolean doTransform)
|
125 |
{
|
126 |
if(dl == null)
|
127 |
return null;
|
128 |
DrawAble dr;
|
129 |
if(doTransform)
|
130 |
{
|
131 |
if(entity.getExtrusion() != 0.0F)
|
132 |
dr = dl.extrude(entity.getExtrusion());
|
133 |
else
|
134 |
dr = dl;
|
135 |
Matrix4D mat = entity.calcArbitMat();
|
136 |
if(mat != null)
|
137 |
dr.transformBy(mat);
|
138 |
} else
|
139 |
if(entity.getExtrusion() != 0.0F)
|
140 |
dr = dl.extrude(entity.getExtrusion(), entity.getUpwardVector());
|
141 |
else
|
142 |
dr = dl;
|
143 |
return dr;
|
144 |
}
|
145 |
|
146 |
public DrawAble extrude(float dist, Vector3D up)
|
147 |
{
|
148 |
if(dist == 0.0F)
|
149 |
return this;
|
150 |
Vector3D ex = new Vector3D(dist * up.x, dist * up.y, dist * up.z);
|
151 |
DrawSet set = new DrawSet(2 + nrPoints);
|
152 |
DrawLines second = new DrawLines(nrPoints);
|
153 |
set.setLayer(super.layer);
|
154 |
set.setColor(super.color);
|
155 |
second.setLayer(super.layer);
|
156 |
second.setColor(super.color);
|
157 |
set.addDrawable(this);
|
158 |
for(int i = 0; i < nrPoints; i++)
|
159 |
{
|
160 |
second.addPoint(line[i].x + ex.x, line[i].y + ex.y, line[i].z + ex.z);
|
161 |
DrawLines conn = new DrawLines(2);
|
162 |
conn.setLayer(super.layer);
|
163 |
conn.setColor(super.color);
|
164 |
conn.addPoint(line[i]);
|
165 |
conn.addPoint(second.line[i]);
|
166 |
set.addDrawable(conn);
|
167 |
}
|
168 |
|
169 |
if(isClosed)
|
170 |
second.close();
|
171 |
set.addDrawable(second);
|
172 |
return set;
|
173 |
}
|
174 |
|
175 |
|
176 |
|
177 |
*/
|
178 |
} |