Mercurial > forge
comparison nonfree/gpc/octave-gpc.cc @ 0:6b33357c7561 octave-forge
Initial revision
author | pkienzle |
---|---|
date | Wed, 10 Oct 2001 19:54:49 +0000 |
parents | |
children | 731e0bfdb40c |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6b33357c7561 |
---|---|
1 /* | |
2 | |
3 Copyright (C) 2001 Rafael Laboissiere | |
4 | |
5 This file is part of octave-gpc. | |
6 | |
7 octave-gpc is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2, or (at your option) any | |
10 later version. | |
11 | |
12 octave-gpc is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with octave-gpc; see the file COPYING. If not, write to the Free | |
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
20 | |
21 */ | |
22 | |
23 #include "octave-gpc.h" | |
24 | |
25 // Registration of the octave_gpc_polygon object type. | |
26 DEFINE_OCTAVE_ALLOCATOR (octave_gpc_polygon); | |
27 | |
28 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_gpc_polygon, "gpc_polygon"); | |
29 | |
30 // Member function | |
31 | |
32 void | |
33 octave_gpc_polygon::print (ostream& os, bool pr_as_read_syntax = false) const | |
34 { | |
35 os << "Variable is of type " << t_name << ".\n" | |
36 << "Its members (accessible with the function gpc_get) are:\n"; | |
37 Octave_map m; | |
38 gpc_to_map (polygon, &m); | |
39 (octave_struct (m)).print (os); | |
40 } | |
41 | |
42 | |
43 // Utility function to check if an Octave object is a matrix with 2 | |
44 // columns. This is the basic type for x.vertices and x.indices, | |
45 // where x is of type gpc_polygon. | |
46 | |
47 static | |
48 bool | |
49 assert_nx2_matrix (octave_value ov) | |
50 { | |
51 if ( ! ov.is_matrix_type () ) | |
52 return false; | |
53 if ( ov.columns () != 2 ) | |
54 return false; | |
55 return true; | |
56 } | |
57 | |
58 // gpc_polygon_free will free any C gpc_polygon structure initialized | |
59 // by the octave_gpc library (in C++). For | |
60 void | |
61 octave_gpc_free_polygon (gpc_polygon* p) | |
62 { | |
63 delete [] p->hole; | |
64 for (int i = 0; i < p->num_contours; i++) | |
65 delete [] p->contour[i].vertex; | |
66 delete [] p->contour; | |
67 } | |
68 | |
69 gpc_polygon* | |
70 get_gpc_pt (octave_value v) | |
71 { | |
72 // Peek the representation and extract the data (i.e., the pointer | |
73 // to the gpc_polygon stored in the octave variable). This seems to | |
74 // be the only way to do that with Octave 2.1 | |
75 const octave_value& rep = v.get_rep (); | |
76 return ((const octave_gpc_polygon&) rep).polygon_value (); | |
77 } | |
78 | |
79 void | |
80 map_to_gpc (Octave_map& m, gpc_polygon* p) | |
81 { | |
82 | |
83 Matrix vtx = m ["vertices"].matrix_value (); | |
84 Matrix idx = m ["indices"].matrix_value (); | |
85 ColumnVector hol = m ["hole"].column_vector_value (); | |
86 int n = idx.rows (); | |
87 | |
88 p->num_contours = n; | |
89 p->hole = new int [n]; | |
90 p->contour = new gpc_vertex_list [n]; | |
91 | |
92 for (int i = 0; i < n; i++) | |
93 { | |
94 p->hole[i] = (int) hol (i); | |
95 | |
96 int j0 = (int) idx (i, 0) - 1; | |
97 int m = (int) idx (i, 1) - j0; | |
98 | |
99 p->contour[i].num_vertices = m; | |
100 p->contour[i].vertex = new gpc_vertex [m]; | |
101 | |
102 for (int j = 0; j < m; j++) | |
103 { | |
104 p->contour[i].vertex[j].x = vtx (j0 + j, 0); | |
105 p->contour[i].vertex[j].y = vtx (j0 + j, 1); | |
106 } | |
107 } | |
108 } | |
109 | |
110 void | |
111 gpc_to_map (gpc_polygon* p, Octave_map* map) | |
112 { | |
113 int n = p->num_contours; | |
114 ColumnVector hol (n); | |
115 Matrix idx (n, 2); | |
116 int m = 0; | |
117 for (int i = 0; i < n; i++) | |
118 { | |
119 hol (i) = p->hole[i]; | |
120 idx (i, 0) = m + 1; | |
121 m += p->contour[i].num_vertices; | |
122 idx (i, 1) = m; | |
123 } | |
124 Matrix vtx (m, 2); | |
125 int j = 0; | |
126 for (int i = 0; i < n; i++) | |
127 { | |
128 int m = p->contour[i].num_vertices; | |
129 gpc_vertex* v = p->contour[i].vertex; | |
130 for (int k = 0; k < m; k++) | |
131 { | |
132 vtx (j, 0) = v[k].x; | |
133 vtx (j++, 1) = v[k].y; | |
134 } | |
135 } | |
136 | |
137 (*map) ["vertices"] = vtx; | |
138 (*map) ["indices"] = idx; | |
139 (*map) ["hole"] = hol; | |
140 } | |
141 | |
142 bool | |
143 assert_gpc_polygon (Octave_map* m) | |
144 { | |
145 if ( ! m->contains ("vertices") ) | |
146 { | |
147 warning ("No vertices !"); | |
148 return false; | |
149 } | |
150 octave_value v = (*m) ["vertices"]; | |
151 if ( ! assert_nx2_matrix (v) ) | |
152 { | |
153 warning ("assert_gpc_polygon: vertices member should be a " | |
154 "[n,2] matrix"); | |
155 return false; | |
156 } | |
157 octave_value i; | |
158 if ( ! m->contains ("indices") ) | |
159 { | |
160 Matrix im (1, 2); | |
161 im (0, 0) = (double) 1; | |
162 im (0, 1) = (double) v.rows (); | |
163 i = (*m) ["indices"] = im; | |
164 } | |
165 else | |
166 { | |
167 i = (*m) ["indices"]; | |
168 if ( ! assert_nx2_matrix (i) ) | |
169 { | |
170 warning ("assert_gpc_polygon: indices member should be a " | |
171 "[n,2] matrix"); | |
172 return false; | |
173 } | |
174 Matrix im = i.matrix_value (); | |
175 if ( im.column_max ().max () > v.rows () | |
176 || im.column_min ().min () < 1 ) | |
177 { | |
178 warning ("assert_gpc_polygon: indices out of range"); | |
179 return false; | |
180 } | |
181 } | |
182 | |
183 if ( ! m->contains ("hole") ) | |
184 { | |
185 ColumnVector h (i.rows ()); | |
186 h.fill ((double) 0); | |
187 (*m) ["hole"] = h; | |
188 } | |
189 else | |
190 { | |
191 octave_value h = (*m) ["hole"]; | |
192 | |
193 if ( (! h.is_matrix_type () || h.columns () != 1) | |
194 && ! h.is_real_scalar () ) | |
195 { | |
196 warning ("assert_gpc_polygon: hole member should be a " | |
197 "column vector"); | |
198 return false; | |
199 } | |
200 int n = h.rows (); | |
201 if ( n != i.rows () ) | |
202 { | |
203 warning ("assert_gpc_polygon: hole member length should " | |
204 "be equal to the number of lines of indices member"); | |
205 return false; | |
206 } | |
207 for (int i = 0; i < n; i++) | |
208 if ( h.matrix_value () (i, 0) != 0 | |
209 && h.matrix_value () (i, 0) != 1 ) | |
210 { | |
211 warning ("assert_gpc_polygon: hole member elements should " | |
212 "be either 0 or 1"); | |
213 return false; | |
214 } | |
215 } | |
216 return true; | |
217 } | |
218 | |
219 /* | |
220 ;;; Local Variables: *** | |
221 ;;; mode: C++ *** | |
222 ;;; End: *** | |
223 */ |