Debian Codegraph @

editor/rotateroom.cpp

raw
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/editor/rotateroom.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:38 $
23 * $Author: kevinb $
24 *
25 *
26 * $Log: not supported by cvs2svn $
27 *
28 * 7 9/01/98 12:04p Matt
29 * Ripped out multi-face portal code
30 *
31 * 6 4/08/98 12:10p Jason
32 * fixed bug where portal faces would get splintered
33 *
34 * 5 12/23/97 1:33p Samir
35 * Added pserror.h
36 *
37 * 4 8/01/97 12:50p Jason
38 * added code to support scaling of rooms/faces/edges
39 *
40 * 3 7/31/97 4:37p Jason
41 * rotate room now triangulates non-planar faces
42 *
43 * 2 7/31/97 3:31p Jason
44 * added functions to rotate portal rooms
45 *
46 * 1 7/31/97 10:35a Jason
47 * file for rotating rooms
48 *
49 * $NoKeywords: $
50 */
51
52 #include "RotateRoom.h"
53 #include "d3edit.h"
54 #include "selectedroom.h"
55 #include "erooms.h"
56 #include "pserror.h"
57
58 void RotateRooms(angle p, angle h, angle b) {
59 int checkfaces[MAX_FACES_PER_ROOM];
60 int checkcount = 0;
61 matrix rotmat, roommat;
62 vector rotpoint, portal_normal;
63 int t, v, i, k;
64 int marked_portalnum = -1;
65 int cur_portalnum = -1;
66
67 if (Curroomp == Markedroomp || Markedroomp == NULL) {
68 OutrageMessageBox("You do not have a valid room marked.");
69 return;
70 }
71
72 for (i = 0; i < Markedroomp->num_portals; i++) {
73 if (Markedroomp->portals[i].croom == (Curroomp - Rooms)) {
74 marked_portalnum = i;
75 break;
76 }
77 }
78
79 if (marked_portalnum == -1) {
80 OutrageMessageBox("The marked room is not connected to the current room!");
81 return;
82 }
83
84 for (i = 0; i < Curroomp->num_portals; i++) {
85 if (Curroomp->portals[i].croom == (Markedroomp - Rooms)) {
86 cur_portalnum = i;
87 break;
88 }
89 }
90
91 if (cur_portalnum == -1) {
92 OutrageMessageBox("The marked room is not connected to the current room!");
93 return;
94 }
95
96 // Save the list so we can use it
97 SaveRoomSelectedList();
98
99 Curroomp->portals[cur_portalnum].croom = -1; // detach base
100 SelectConnectedRooms(Curroomp - Rooms);
101 Curroomp->portals[cur_portalnum].croom = Markedroomp - Rooms;
102
103 // Make sure valid rooms are selected
104 if (IsRoomSelected(Markedroomp - Rooms)) {
105 RestoreRoomSelectedList();
106 OutrageMessageBox("Cannot rotate: rooms connect back to base room.");
107 return;
108 }
109
110 // Compute point & matrix for rotation
111 ComputePortalCenter(&rotpoint, Curroomp, cur_portalnum);
112 vm_AnglesToMatrix(&rotmat, p, h, b); // get desired rotation
113 face *fp = &Curroomp->faces[Curroomp->portals[cur_portalnum].portal_face];
114 ComputeNormal(&portal_normal, fp->num_verts, fp->face_verts, Curroomp->verts);
115 portal_normal *= -1.0; // reverse it!
116
117 vm_VectorToMatrix(&roommat, &portal_normal, NULL, NULL);
118 rotmat = roommat * ~rotmat * ~roommat; // get rotation relative to start room
119
120 // Now rotate all the points
121
122 for (i = 0; i < N_selected_rooms; i++) {
123 room *rp = &Rooms[Selected_rooms[i]];
124
125 for (v = 0; v < rp->num_verts; v++) {
126 if (rp == Curroomp) {
127 face *fp = &Curroomp->faces[Curroomp->portals[cur_portalnum].portal_face];
128 for (t = 0; t < fp->num_verts; t++)
129 if (v == fp->face_verts[t])
130 goto Skipit;
131 }
132 rp->verts[v] = ((rp->verts[v] - rotpoint) * rotmat) + rotpoint;
133
134 Skipit:;
135 }
136
137 // Rotate all the normals
138 for (t = 0; t < rp->num_faces; t++)
139 rp->faces[t].normal = rp->faces[t].normal * rotmat;
140 }
141
142 // Now I guess we're done
143 RestoreRoomSelectedList();
144
145 // Go through face list and build a list of faces that touch the vertices
146 // that make up the portal we're modifying...
147 fp = &Curroomp->faces[Curroomp->portals[cur_portalnum].portal_face];
148 for (i = 0; i < fp->num_verts; i++) {
149 int checkvert = fp->face_verts[i];
150
151 for (t = 0; t < Curroomp->num_faces; t++) {
152 face *fp = &Curroomp->faces[t];
153
154 for (int l = 0; l < Curroomp->num_portals; l++)
155 if (Curroomp->portals[l].portal_face == t)
156 goto skipface;
157
158 for (v = 0; v < fp->num_verts; v++) {
159 if (fp->face_verts[v] == checkvert) {
160 for (k = 0; k < checkcount; k++) {
161 if (checkfaces[k] == t)
162 break;
163 }
164 if (k == checkcount) {
165 checkfaces[checkcount] = t;
166 checkcount++;
167 }
168 }
169 }
170 skipface:;
171 }
172 }
173
174 // Now fix them!
175 if (checkcount > 0)
176 FixConcaveFaces(Curroomp, checkfaces, checkcount);
177
178 // If the rotated seg is floating, recompute uvs for connected seg
179 // CalcFloatUVs(&Segments[baseseg],baseside);
180
181 // Set flags
182 World_changed = 1;
183
184 return;
185 }
186
187 /*
188 //Move a floating segment (and all the segments attached to it)
189 //Parameters: dx,dy - how much to move the segment, releative to its right & up vectors
190 void MoveFloatSegment(float dx,float dy)
191 {
192 uint8_t selected_vert[MAX_VERTICES];
193 matrix segmat;
194 vector deltav;
195 int baseseg,baseside;
196 int s,v,i;
197 segment *segp;
198
199 if (! (Cursegp->sides[Curside].flags & SDF_FLOATING)) {
200 OutrageMessageBox("Curseg/Curside must be floating for this operation.");
201 return;
202 }
203
204 //Save the list so we can use it
205 SaveSelectedList();
206
207 //Select all segments for rotation
208 baseseg = Cursegp->children[Curside];
209 Cursegp->children[Curside] = -1; //detach base
210 SelectConnectedSegs(Cursegp-Segments);
211 Cursegp->children[Curside] = baseseg; //re-attach base
212 baseside = FindConnectSide(Cursegp,&Segments[baseseg]);
213
214 //Make sure valid segments are selected
215 if (IsSelected(baseseg)) {
216 RestoreSelectedList();
217 OutrageMessageBox("Cannot rotate: segments connect back to base segment.");
218 return;
219 }
220
221 //Flag vertices to rotate
222 for (v=0;v<MAX_VERTICES;v++)
223 selected_vert[v] = 0;
224
225 for (i=0;i<N_selected_segs;i++) {
226 segp = &Segments[Selected_segs[i]];
227 for (v=0;v<VPS;v++)
228 selected_vert[segp->verts[v]] = 1;
229 }
230
231 //Check and fix any selected verts used by non-selected segments
232 for (s=0,segp=Segments;s<=Highest_segment_index;s++,segp)
233 if (! segp->flags & SF_UNUSED_SEGMENT) {
234 for (v=0;v<VPS;v++) {
235 int vertnum = segp->verts[v];
236 if (selected_vert[vertnum]) { //shared vert!
237 int new_vertnum;
238 new_vertnum = AddDuplicateVertex(&Vertices[segp->verts[v]]);
239 selected_vert[vertnum] = 0; //old is not in list
240 selected_vert[new_vertnum] = 1; //new is in list
241
242 //Go through selected list, & remap verts
243 for (i=0;i<N_selected_segs;i++) {
244 segment *segp = &Segments[Selected_segs[i]];
245 for (v=0;v<VPS;v++)
246 if (segp->verts[v] == vertnum)
247 segp->verts[v] = new_vertnum;
248 }
249 }
250 }
251 }
252
253 //Compute the delta vector
254 ExtractMatrixFromSeg(Cursegp,Side_opposite[Curside],&segmat);
255 deltav = segmat.uvec * dy + segmat.rvec * dx;
256
257 //Now translate all the points
258 for (v=0;v<MAX_VERTICES;v++)
259 if (selected_vert[v])
260 Vertices[v] += deltav;
261
262 //Now move all the objects in the segments
263 for (s=0;s<N_selected_segs;s++) {
264 int o;
265 for (o=Segments[Selected_segs[s]].objects;o!=-1;o=Objects[o].next)
266 Objects[o].pos += deltav;
267 }
268
269 //Now I guess we're done
270 RestoreSelectedList();
271
272 //Fix up modified segment
273 ValidateSegment(Cursegp);
274
275 //Warn user if segment broken
276 WarnIfConcaveSegment(Cursegp);
277
278 //If the rotated seg is floating, recompute uvs for connected seg
279 CalcFloatUVs(&Segments[baseseg],baseside);
280
281 //Set flags
282 World_changed = 1;
283
284 return;
285 }
286
287
288 // Rotates a segment
289 // segment must have 1 and only 1 connection for this to work.
290 int RotateSegment(segment *seg, angle p, angle h, angle b)
291 {
292 vector rotpoint;
293 matrix segmat;
294 matrix rotmat;
295 int v, s, count, attside, rotside;
296 int o,side;
297
298 // Find side of attachment and side to be rotated.
299 count = 0;
300 for (s=0; s<SIDES_PER_SEGMENT; s++)
301 if (IS_CHILD(seg->children[s])) {
302 count++;
303 attside = s;
304 }
305
306 // rotside = 0;
307 // while ((destseg->children[destside] != seg-Segments) && (destside < MAX_SIDES_PER_SEGMENT))
308 // destside++;
309 rotside = Side_opposite[attside];
310
311 // Return if passed in segment is connected to other than 1 segment.
312 if (count != 1) return 1;
313
314 vm_AnglesToMatrix(&rotmat, p,h,b);
315
316 ComputeCenterPointOnSide(&rotpoint,seg,attside);
317 ExtractMatrixFromSeg(seg,rotside,&segmat); //get this segment's orientation
318 rotmat = segmat * ~rotmat * ~segmat; //get rotation relative to start segment
319
320 // remap free vertices to the new orientation
321 for (v = 0; v < 4; v++)
322 {
323 Vertices[seg->verts[Side_to_verts[rotside][v]]] =
324 ((Vertices[seg->verts[Side_to_verts[rotside][v]]] - rotpoint) * rotmat) + rotpoint;
325 }
326
327 // Now rotate the surface normals and the objects in the segments
328 for (o=seg->objects;o!=-1;o=Objects[o].next)
329 Objects[o].pos = ((Objects[o].pos - rotpoint) * rotmat) + rotpoint;
330 for (side=0;side<SPS;side++)
331 {
332 seg->sides[side].normals[0] = seg->sides[side].normals[0] * rotmat;
333 seg->sides[side].normals[1] = seg->sides[side].normals[1] * rotmat;
334 }
335
336 // Fix up modified segment
337 ValidateSegment(seg);
338
339 // Warn user if segment broken
340 WarnIfConcaveSegment(seg);
341
342 // Set flags
343 World_changed = 1;
344
345 return 0;
346 }
347 */