OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
SculptMesh.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 using System.Collections.Generic;
30 using System.Text;
31 using System.IO;
32 
33 using System.Drawing;
34 using System.Drawing.Imaging;
35 
36 namespace PrimMesher
37 {
38 
39  public class SculptMesh
40  {
41  public List<Coord> coords;
42  public List<Face> faces;
43 
44  public enum SculptType { sphere = 1, torus = 2, plane = 3, cylinder = 4 };
45 
46 
47  public SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool mirror, bool invert)
48  {
49  if (mirror)
50  invert = !invert;
51 
52  SculptMap smap = new SculptMap(sculptBitmap, lod);
53 
54  List<List<Coord>> rows = smap.ToRows(mirror);
55 
56  _SculptMesh(rows, sculptType, invert);
57  }
58 
59  private void _SculptMesh(List<List<Coord>> rows, SculptType sculptType, bool invert)
60  {
61  coords = new List<Coord>();
62  faces = new List<Face>();
63 
64  sculptType = (SculptType)(((int)sculptType) & 0x07);
65 
66  int width = rows[0].Count;
67 
68  int p1, p2, p3, p4;
69 
70  int imageX, imageY;
71 
72  if (sculptType != SculptType.plane)
73  {
74  if (rows.Count % 2 == 0)
75  {
76  for (int rowNdx = 0; rowNdx < rows.Count; rowNdx++)
77  rows[rowNdx].Add(rows[rowNdx][0]);
78  }
79  else
80  {
81  int lastIndex = rows[0].Count - 1;
82 
83  for (int i = 0; i < rows.Count; i++)
84  rows[i][0] = rows[i][lastIndex];
85  }
86  }
87 
88  Coord topPole = rows[0][width / 2];
89  Coord bottomPole = rows[rows.Count - 1][width / 2];
90 
91  if (sculptType == SculptType.sphere)
92  {
93  if (rows.Count % 2 == 0)
94  {
95  int count = rows[0].Count;
96  List<Coord> topPoleRow = new List<Coord>(count);
97  List<Coord> bottomPoleRow = new List<Coord>(count);
98 
99  for (int i = 0; i < count; i++)
100  {
101  topPoleRow.Add(topPole);
102  bottomPoleRow.Add(bottomPole);
103  }
104  rows.Insert(0, topPoleRow);
105  rows.Add(bottomPoleRow);
106  }
107  else
108  {
109  int count = rows[0].Count;
110 
111  List<Coord> topPoleRow = rows[0];
112  List<Coord> bottomPoleRow = rows[rows.Count - 1];
113 
114  for (int i = 0; i < count; i++)
115  {
116  topPoleRow[i] = topPole;
117  bottomPoleRow[i] = bottomPole;
118  }
119  }
120  }
121 
122  if (sculptType == SculptType.torus)
123  rows.Add(rows[0]);
124 
125  int coordsDown = rows.Count;
126  int coordsAcross = rows[0].Count;
127 
128  float widthUnit = 1.0f / (coordsAcross - 1);
129  float heightUnit = 1.0f / (coordsDown - 1);
130 
131  for (imageY = 0; imageY < coordsDown; imageY++)
132  {
133  int rowOffset = imageY * coordsAcross;
134 
135  for (imageX = 0; imageX < coordsAcross; imageX++)
136  {
137  /*
138  * p1-----p2
139  * | \ f2 |
140  * | \ |
141  * | f1 \|
142  * p3-----p4
143  */
144 
145  p4 = rowOffset + imageX;
146  p3 = p4 - 1;
147 
148  p2 = p4 - coordsAcross;
149  p1 = p3 - coordsAcross;
150 
151  this.coords.Add(rows[imageY][imageX]);
152 
153  if (imageY > 0 && imageX > 0)
154  {
155  Face f1, f2;
156 
157  if (invert)
158  {
159  f1 = new Face(p1, p4, p3);
160  f2 = new Face(p1, p2, p4);
161  }
162  else
163  {
164  f1 = new Face(p1, p3, p4);
165  f2 = new Face(p1, p4, p2);
166  }
167 
168  this.faces.Add(f1);
169  this.faces.Add(f2);
170  }
171  }
172  }
173  }
174 
179  public SculptMesh Copy()
180  {
181  return new SculptMesh(this);
182  }
183 
185  {
186  coords = new List<Coord>(sm.coords);
187  faces = new List<Face>(sm.faces);
188  }
189 
190  public void Scale(float x, float y, float z)
191  {
192  int i;
193  int numVerts = this.coords.Count;
194 
195  Coord m = new Coord(x, y, z);
196  for (i = 0; i < numVerts; i++)
197  this.coords[i] *= m;
198  }
199 
200  public void DumpRaw(String path, String name, String title)
201  {
202  if (path == null)
203  return;
204  String fileName = name + "_" + title + ".raw";
205  String completePath = System.IO.Path.Combine(path, fileName);
206  StreamWriter sw = new StreamWriter(completePath);
207 
208  for (int i = 0; i < this.faces.Count; i++)
209  {
210  string s = this.coords[this.faces[i].v1].ToString();
211  s += " " + this.coords[this.faces[i].v2].ToString();
212  s += " " + this.coords[this.faces[i].v3].ToString();
213 
214  sw.WriteLine(s);
215  }
216 
217  sw.Close();
218  }
219  }
220 }
List< Face > faces
Definition: SculptMesh.cs:47
void DumpRaw(String path, String name, String title)
Definition: SculptMesh.cs:200
List< Coord > coords
Definition: SculptMesh.cs:46
SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool mirror, bool invert)
Definition: SculptMesh.cs:47
OpenSim.Region.OptionalModules.Scripting.Minimodule.Object.SculptType SculptType
Definition: SOPObject.cs:39
SculptMesh(string fileName, int sculptType, int lod, int viewerMode, int mirror, int invert)
Definition: SculptMesh.cs:66
void Scale(float x, float y, float z)
Definition: SculptMesh.cs:190
SculptMesh(SculptMesh sm)
Definition: SculptMesh.cs:184
SculptMesh Copy()
Duplicates a SculptMesh object. All object properties are copied by value, including lists...
Definition: SculptMesh.cs:179