Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / objects / DwgSpline.java @ 2896

History | View | Annotate | Download (11.6 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 com.iver.cit.jdwglib.dwg.objects;
36

    
37
import java.util.Vector;
38

    
39
import com.iver.cit.jdwglib.dwg.DwgObject;
40
import com.iver.cit.jdwglib.dwg.DwgUtil;
41

    
42
/**
43
 * The DwgSpline class represents a DWG Spline
44
 * 
45
 * @author jmorell
46
 */
47
public class DwgSpline extends DwgObject {
48
        private int scenario;
49
        private int degree;
50
        private double fitTolerance;
51
        private double[] beginTanVector;
52
        private double[] endTanVector;
53
        private boolean rational;
54
        private boolean closed;
55
        private boolean periodic;
56
        private double knotTolerance;
57
        private double controlTolerance;
58
        private double[] knotPoints;
59
        private double[][] controlPoints;
60
        private double[] weights;
61
        private double[][] fitPoints;
62
        
63
        /**
64
         * Read a Spline in the DWG format Version 15
65
         * 
66
         * @param data Array of unsigned bytes obtained from the DWG binary file
67
         * @param offset The current bit offset where the value begins
68
         * @throws Exception If an unexpected bit value is found in the DWG file. Occurs
69
         *                    when we are looking for LwPolylines.
70
         */
71
        public void readDwgSplineV15(int[] data, int offset) throws Exception {
72
                int bitPos = offset;
73
                bitPos = readObjectHeaderV15(data, bitPos);
74
                Vector v = DwgUtil.getBitShort(data, bitPos);
75
                bitPos = ((Integer)v.get(0)).intValue();
76
                int sc = ((Integer)v.get(1)).intValue();
77
                scenario = sc;
78
                v = DwgUtil.getBitShort(data, bitPos);
79
                bitPos = ((Integer)v.get(0)).intValue();
80
                int deg = ((Integer)v.get(1)).intValue();
81
                degree = deg;
82
                int knotsNumber = 0;
83
                int controlPointsNumber = 0;
84
                int fitPointsNumber = 0;
85
                boolean weight = false;
86
                if (sc==2) {
87
                        v = DwgUtil.getBitDouble(data, bitPos);
88
                        bitPos = ((Integer)v.get(0)).intValue();
89
                        double ft = ((Double)v.get(1)).doubleValue();
90
                        fitTolerance = ft;
91
                        v = DwgUtil.getBitDouble(data, bitPos);
92
                        bitPos = ((Integer)v.get(0)).intValue();
93
                        double x = ((Double)v.get(1)).doubleValue();
94
                        v = DwgUtil.getBitDouble(data, bitPos);
95
                        bitPos = ((Integer)v.get(0)).intValue();
96
                        double y = ((Double)v.get(1)).doubleValue();
97
                        v = DwgUtil.getBitDouble(data, bitPos);
98
                        bitPos = ((Integer)v.get(0)).intValue();
99
                        double z = ((Double)v.get(1)).doubleValue();
100
                        double[] coord = new double[]{x, y, z};
101
                        beginTanVector = coord;
102
                        v = DwgUtil.getBitDouble(data, bitPos);
103
                        bitPos = ((Integer)v.get(0)).intValue();
104
                        x = ((Double)v.get(1)).doubleValue();
105
                        v = DwgUtil.getBitDouble(data, bitPos);
106
                        bitPos = ((Integer)v.get(0)).intValue();
107
                        y = ((Double)v.get(1)).doubleValue();
108
                        v = DwgUtil.getBitDouble(data, bitPos);
109
                        bitPos = ((Integer)v.get(0)).intValue();
110
                        z = ((Double)v.get(1)).doubleValue();
111
                        coord = new double[]{x, y, z};
112
                        endTanVector = coord;
113
                        v = DwgUtil.getBitShort(data, bitPos);
114
                        bitPos = ((Integer)v.get(0)).intValue();
115
                        fitPointsNumber = ((Integer)v.get(1)).intValue();
116
                } else if (sc==1) {
117
                        v = DwgUtil.testBit(data, bitPos);
118
                        bitPos = ((Integer)v.get(0)).intValue();
119
                        boolean rat = ((Boolean)v.get(1)).booleanValue();
120
                        rational = rat;
121
                        v = DwgUtil.testBit(data, bitPos);
122
                        bitPos = ((Integer)v.get(0)).intValue();
123
                        boolean closed = ((Boolean)v.get(1)).booleanValue();
124
                        this.closed = closed;
125
                        v = DwgUtil.testBit(data, bitPos);
126
                        bitPos = ((Integer)v.get(0)).intValue();
127
                        boolean per = ((Boolean)v.get(1)).booleanValue();
128
                        periodic = per;
129
                        v = DwgUtil.getBitDouble(data, bitPos);
130
                        bitPos = ((Integer)v.get(0)).intValue();
131
                        double ktol = ((Double)v.get(1)).doubleValue();
132
                        knotTolerance = ktol;
133
                        v = DwgUtil.getBitDouble(data, bitPos);
134
                        bitPos = ((Integer)v.get(0)).intValue();
135
                        double ctol = ((Double)v.get(1)).doubleValue();
136
                        controlTolerance = ctol;
137
                        v = DwgUtil.getBitLong(data, bitPos);
138
                        bitPos = ((Integer)v.get(0)).intValue();
139
                        knotsNumber = ((Integer)v.get(1)).intValue();
140
                        v = DwgUtil.getBitLong(data, bitPos);
141
                        bitPos = ((Integer)v.get(0)).intValue();
142
                        controlPointsNumber = ((Integer)v.get(1)).intValue();
143
                        v = DwgUtil.testBit(data, bitPos);
144
                        bitPos = ((Integer)v.get(0)).intValue();
145
                        weight = ((Boolean)v.get(1)).booleanValue();
146
                } else {
147
                        System.out.println("ERROR: Escenario desconocido");
148
                }
149
                if (knotsNumber>0) {
150
                        double[] knotpts = new double[knotsNumber];
151
                        for (int i=0;i<knotsNumber;i++) {
152
                                v = DwgUtil.getBitDouble(data, bitPos);
153
                                bitPos = ((Integer)v.get(0)).intValue();
154
                                knotpts[i] = ((Double)v.get(1)).doubleValue();
155
                        }
156
                        knotPoints = knotpts;
157
                }
158
                if (controlPointsNumber>0) {
159
                        // Si el n?mero de weights no coincide con el de ctrlpts habr? problemas ...
160
                        double[][] ctrlpts = new double[controlPointsNumber][3];
161
                        double[] weights = new double[controlPointsNumber];
162
                        for (int i=0;i<controlPointsNumber;i++) {
163
                                v = DwgUtil.getBitDouble(data, bitPos);
164
                                bitPos = ((Integer)v.get(0)).intValue();
165
                                double x = ((Double)v.get(1)).doubleValue();
166
                                v = DwgUtil.getBitDouble(data, bitPos);
167
                                bitPos = ((Integer)v.get(0)).intValue();
168
                                double y = ((Double)v.get(1)).doubleValue();
169
                                v = DwgUtil.getBitDouble(data, bitPos);
170
                                bitPos = ((Integer)v.get(0)).intValue();
171
                                double z = ((Double)v.get(1)).doubleValue();
172
                                //double[] coord = new double[]{x, y, z};
173
                                ctrlpts[i][0] = x;
174
                                ctrlpts[i][1] = y;
175
                                ctrlpts[i][2] = z;
176
                                if (weight) {
177
                                        v = DwgUtil.getBitDouble(data, bitPos);
178
                                        bitPos = ((Integer)v.get(0)).intValue();
179
                                        weights[i] = ((Double)v.get(1)).doubleValue();
180
                                }
181
                        }
182
                        controlPoints = ctrlpts;
183
                        if (weight) {
184
                                this.weights = weights;
185
                        }
186
                }
187
                if (fitPointsNumber>0) {
188
                        double[][] fitpts = new double[fitPointsNumber][3];
189
                        for (int i=0;i<fitPointsNumber;i++) {
190
                                v = DwgUtil.getBitDouble(data, bitPos);
191
                                bitPos = ((Integer)v.get(0)).intValue();
192
                                double x = ((Double)v.get(1)).doubleValue();
193
                                v = DwgUtil.getBitDouble(data, bitPos);
194
                                bitPos = ((Integer)v.get(0)).intValue();
195
                                double y = ((Double)v.get(1)).doubleValue();
196
                                v = DwgUtil.getBitDouble(data, bitPos);
197
                                bitPos = ((Integer)v.get(0)).intValue();
198
                                double z = ((Double)v.get(1)).doubleValue();
199
                                fitpts[i][0] = x;
200
                                fitpts[i][1] = y;
201
                                fitpts[i][2] = z;
202
                        }
203
                        fitPoints = fitpts;
204
                }
205
                bitPos = readObjectTailV15(data, bitPos);
206
        }
207
        /**
208
         * @return Returns the closed.
209
         */
210
        public boolean isClosed() {
211
                return closed;
212
        }
213
        /**
214
         * @param closed The closed to set.
215
         */
216
        public void setClosed(boolean closed) {
217
                this.closed = closed;
218
        }
219
        /**
220
         * @return Returns the controlPoints.
221
         */
222
        public double[][] getControlPoints() {
223
                return controlPoints;
224
        }
225
        /**
226
         * @param controlPoints The controlPoints to set.
227
         */
228
        public void setControlPoints(double[][] controlPoints) {
229
                this.controlPoints = controlPoints;
230
        }
231
        /**
232
         * @return Returns the fitPoints.
233
         */
234
        public double[][] getFitPoints() {
235
                return fitPoints;
236
        }
237
        /**
238
         * @param fitPoints The fitPoints to set.
239
         */
240
        public void setFitPoints(double[][] fitPoints) {
241
                this.fitPoints = fitPoints;
242
        }
243
        /**
244
         * @return Returns the knotPoints.
245
         */
246
        public double[] getKnotPoints() {
247
                return knotPoints;
248
        }
249
        /**
250
         * @param knotPoints The knotPoints to set.
251
         */
252
        public void setKnotPoints(double[] knotPoints) {
253
                this.knotPoints = knotPoints;
254
        }
255
        /**
256
         * @return Returns the scenario.
257
         */
258
        public int getScenario() {
259
                return scenario;
260
        }
261
        /**
262
         * @param scenario The scenario to set.
263
         */
264
        public void setScenario(int scenario) {
265
                this.scenario = scenario;
266
        }
267
        /* (non-Javadoc)
268
         * @see java.lang.Object#clone()
269
         */
270
        public Object clone() {
271
                DwgSpline dwgSpline = new DwgSpline();
272
                dwgSpline.setType(type);
273
                dwgSpline.setHandle(handle);
274
                dwgSpline.setVersion(version);
275
                dwgSpline.setMode(mode);
276
                dwgSpline.setLayerHandle(layerHandle);
277
                dwgSpline.setColor(color);
278
                dwgSpline.setNumReactors(numReactors);
279
                dwgSpline.setNoLinks(noLinks);
280
                dwgSpline.setLinetypeFlags(linetypeFlags);
281
                dwgSpline.setPlotstyleFlags(plotstyleFlags);
282
                dwgSpline.setSizeInBits(sizeInBits);
283
                dwgSpline.setExtendedData(extendedData);
284
                dwgSpline.setGraphicData(graphicData);
285
                //dwgSpline.setInsideBlock(insideBlock);
286
                dwgSpline.setScenario(scenario);
287
                dwgSpline.setDegree(degree);
288
                dwgSpline.setFitTolerance(fitTolerance);
289
                dwgSpline.setBeginTanVector(beginTanVector);
290
                dwgSpline.setEndTanVector(endTanVector);
291
                dwgSpline.setRational(rational);
292
                dwgSpline.setClosed(closed);
293
                dwgSpline.setPeriodic(periodic);
294
                dwgSpline.setKnotTolerance(knotTolerance);
295
                dwgSpline.setControlTolerance(controlTolerance);
296
                dwgSpline.setKnotPoints(knotPoints);
297
                dwgSpline.setControlPoints(controlPoints);
298
                dwgSpline.setWeights(weights);
299
                dwgSpline.setFitPoints(fitPoints);
300
                return dwgSpline;
301
        }
302
        /**
303
         * @return Returns the beginTanVector.
304
         */
305
        public double[] getBeginTanVector() {
306
                return beginTanVector;
307
        }
308
        /**
309
         * @param beginTanVector The beginTanVector to set.
310
         */
311
        public void setBeginTanVector(double[] beginTanVector) {
312
                this.beginTanVector = beginTanVector;
313
        }
314
        /**
315
         * @return Returns the controlTolerance.
316
         */
317
        public double getControlTolerance() {
318
                return controlTolerance;
319
        }
320
        /**
321
         * @param controlTolerance The controlTolerance to set.
322
         */
323
        public void setControlTolerance(double controlTolerance) {
324
                this.controlTolerance = controlTolerance;
325
        }
326
        /**
327
         * @return Returns the degree.
328
         */
329
        public int getDegree() {
330
                return degree;
331
        }
332
        /**
333
         * @param degree The degree to set.
334
         */
335
        public void setDegree(int degree) {
336
                this.degree = degree;
337
        }
338
        /**
339
         * @return Returns the endTanVector.
340
         */
341
        public double[] getEndTanVector() {
342
                return endTanVector;
343
        }
344
        /**
345
         * @param endTanVector The endTanVector to set.
346
         */
347
        public void setEndTanVector(double[] endTanVector) {
348
                this.endTanVector = endTanVector;
349
        }
350
        /**
351
         * @return Returns the fitTolerance.
352
         */
353
        public double getFitTolerance() {
354
                return fitTolerance;
355
        }
356
        /**
357
         * @param fitTolerance The fitTolerance to set.
358
         */
359
        public void setFitTolerance(double fitTolerance) {
360
                this.fitTolerance = fitTolerance;
361
        }
362
        /**
363
         * @return Returns the knotTolerance.
364
         */
365
        public double getKnotTolerance() {
366
                return knotTolerance;
367
        }
368
        /**
369
         * @param knotTolerance The knotTolerance to set.
370
         */
371
        public void setKnotTolerance(double knotTolerance) {
372
                this.knotTolerance = knotTolerance;
373
        }
374
        /**
375
         * @return Returns the periodic.
376
         */
377
        public boolean isPeriodic() {
378
                return periodic;
379
        }
380
        /**
381
         * @param periodic The periodic to set.
382
         */
383
        public void setPeriodic(boolean periodic) {
384
                this.periodic = periodic;
385
        }
386
        /**
387
         * @return Returns the rational.
388
         */
389
        public boolean isRational() {
390
                return rational;
391
        }
392
        /**
393
         * @param rational The rational to set.
394
         */
395
        public void setRational(boolean rational) {
396
                this.rational = rational;
397
        }
398
        /**
399
         * @return Returns the weights.
400
         */
401
        public double[] getWeights() {
402
                return weights;
403
        }
404
        /**
405
         * @param weights The weights to set.
406
         */
407
        public void setWeights(double[] weights) {
408
                this.weights = weights;
409
        }
410
}