summaryrefslogtreecommitdiff
path: root/trunk/reprap/miscellaneous/adrians-java/Polygon-java/rr_p_list.java
blob: 0d349ccda27aad9c9c8e34d512b3f8c7b78f3b0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*

RepRap
------

The Replicating Rapid Prototyper Project


Copyright (C) 2005
Adrian Bowyer & The University of Bath

http://reprap.org

Principal author:

Adrian Bowyer
Department of Mechanical Engineering
Faculty of Engineering and Design
University of Bath
Bath BA2 7AY
U.K.

e-mail: A.Bowyer@bath.ac.uk

RepRap is free; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
Licence as published by the Free Software Foundation; either
version 2 of the Licence, or (at your option) any later version.

RepRap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public Licence for more details.

For this purpose the words "software" and "library" in the GNU Library
General Public Licence are taken to mean any and all computer programs
computer files data results documents and other copyright information
available from the RepRap project.

You should have received a copy of the GNU Library General Public
Licence along with RepRap; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA,
or see

http://www.gnu.org/

=====================================================================


rr_p_list: A collection of 2D polygons

First version 20 May 2005
This version: 8 March 2006

List of polygons class.  This too maintains a maximum enclosing rectangle.
Each polygon has an associated type that can be used to record any attribute
of the polygon.

*/


import java.io.*;
import java.util.*;


class rr_p_list
{
    public List polygons;
    public rr_box box;

    public rr_p_list()
    {
	polygons = new ArrayList();
	box = new rr_box();
    }

    // Deep copy

    public rr_p_list(rr_p_list lst)
    {
	polygons = new ArrayList();
	box = new rr_box(lst.box);
	int leng = lst.polygons.size();
	for(int i = 0; i < leng; i++)
	    polygons.add(new rr_polygon((rr_polygon)lst.polygons.get(i)));
    }


    // Put a new list on the end

    public void append(rr_p_list lst)
    {
	int leng = lst.polygons.size();
	if(leng == 0)
	    return;
	for(int i = 0; i < leng; i++)
		polygons.add(new rr_polygon((rr_polygon)lst.polygons.get(i)));
	box.expand(lst.box);
    }

    // Add one new polygon to the list

    public void append(rr_polygon p)
    {
	append(p.no_cross());
    }

    /*
    // Booleans on polygons

    public rr_p_list union(rr_p_list a,  rr_p_list b)
    {

    }

    public rr_p_list intersection(rr_p_list a,  rr_p_list b)
    {

    }


    public rr_p_list difference(rr_p_list a,  rr_p_list b)
    {
       return intersection(a, negate(b));
    }
    */
    
    // Negate all the polygons
    
    public rr_p_list negate()
    {
        rr_p_list result = new rr_p_list();
  	int leng = polygons.size();
	for(int i = 0; i < leng; i++)
        {
            result.polygons.add(((rr_polygon)polygons.get(i)).negate());
        }
        result.box = new rr_box(box);
        return result;
    }

    // Write as an SVG xml to file opf

    public void svg(PrintStream opf)
    {
        opf.println("<?xml version=\"1.0\" standalone=\"no\"?>");
        opf.println("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"");
        opf.println("\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">");
        opf.println("<svg");
        opf.println(" width=\"" + Double.toString(box.x.length()) + "mm\"");
        opf.println(" height=\""  + Double.toString(box.y.length()) +  "mm\"");
        opf.print(" viewBox=\"" + Double.toString(box.x.low));
        opf.print(" " + Double.toString(box.y.low));
        opf.print(" " + Double.toString(box.x.high));
	opf.println(" " + Double.toString(box.y.high) + "\"");
        opf.println(" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
	opf.println(" <desc>RepRap polygon list - http://reprap.org</desc>");

	int leng = polygons.size();
        for(int i = 0; i < leng; i++)
            ((rr_polygon)polygons.get(i)).svg(opf);

        opf.println("</svg>");
    }


    // Simplify all polygons by length d
    // N.B. this may throw away small ones completely

    public rr_p_list simplify(double d)
    {
        rr_p_list r = new rr_p_list();
        int leng = polygons.size();
        double d2 = d*d;

	for(int i = 0; i < leng; i++)
	    {
		rr_polygon p = (rr_polygon)polygons.get(i);
		if(p.box.d_2() > 2*d2)
		    r.append(p.simplify(d));
	    }

        return r;
    }


    // Intersect a line with a polygon list, returning an
    // unsorted list of the intersection parameters

    public List pl_intersect(rr_line l0)
    {
        int leng = polygons.size();
        List t = new ArrayList();

        for(int i = 0; i < leng; i++)
	    {
		List t1 = ((rr_polygon)polygons.get(i)).pl_intersect(l0);
		int leng1 = t1.size();
		for(int j = 0; j < leng1; j++)
		    t.add(t1.get(j));
	    }
	return t;
    }


    // Offset every polygon in the list

    public rr_p_list offset(double d)
    {
	int leng = polygons.size();
        rr_p_list r = new rr_p_list();
        for (int i = 0; i < leng; i++)
	    r.append(((rr_polygon)polygons.get(i)).offset(d));
	return r;
    }


    // Hatch a polygon list parallel to line l0 with index gap
    // Returning a polygon as the result with flag values f
    
    public rr_polygon hatch(rr_line l0, double gap, int fg, int fs)
    {
	rr_box big = box.scale(1.1);
	double d = Math.sqrt(big.d_2());
	rr_polygon r = new rr_polygon();
	rr_2point orth = new rr_2point(-l0.direction.y, l0.direction.x);
	orth.norm();

	int quad = (int)(2*Math.atan2(orth.y, orth.x)/Math.PI);

	rr_2point org;

	switch(quad)
	    {
	    case 0:
		org = big.min();
		break;

	    case 1:
		org = new rr_2point(big.x.high, big.y.low);
		break;

	    case 2:
		org = big.max();  // Do you want fries with that?  
		break;

	    case 3:
		org = new rr_2point(big.x.low, big.y.high);
		break;

	    default:
		System.err.println("rr_polygon hatch(): The atan2 function doesn't seem to work...");
		org = big.min();
	    }

        double g = 0;
        int i = 0;
	orth = orth.mul(gap);

	rr_line hatcher = new rr_line(org, org.add(l0.direction));

        while (g < d)
	    {
		hatcher = hatcher.neg();
		List t_vals = pl_intersect(hatcher);
		if (t_vals.size() > 0)
		    r.append(rr_polygon.rr_t_polygon(t_vals, hatcher, fg, fs));
		hatcher = hatcher.add(orth);
		g = g + gap;
	    }
	r.flags.set(0, new Integer(0));
        return r;
    }
        

}