Update meshing code to sync with current PrimMesher.cs on forge.
Migrate sculpt meshing code to primMesher version. This should result in more accurate physical sculpted prim proxies. Remove much obsolete code from Region/Physics/Meshing0.6.1-post-fixes
parent
1952a67ceb
commit
fdd2388331
|
@ -1,472 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) Contributors, http://opensimulator.org/
|
||||
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of the OpenSim Project nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
//#define SPAM
|
||||
|
||||
using OpenMetaverse;
|
||||
using OpenSim.Region.Physics.Manager;
|
||||
|
||||
namespace OpenSim.Region.Physics.Meshing
|
||||
{
|
||||
internal class Extruder
|
||||
{
|
||||
//public float startParameter;
|
||||
//public float stopParameter;
|
||||
public PhysicsVector size;
|
||||
|
||||
public float taperTopFactorX = 1f;
|
||||
public float taperTopFactorY = 1f;
|
||||
public float taperBotFactorX = 1f;
|
||||
public float taperBotFactorY = 1f;
|
||||
|
||||
public float pushX = 0f;
|
||||
public float pushY = 0f;
|
||||
|
||||
// twist amount in radians. NOT DEGREES.
|
||||
public float twistTop = 0;
|
||||
public float twistBot = 0;
|
||||
public float twistMid = 0;
|
||||
public float pathScaleX = 1.0f;
|
||||
public float pathScaleY = 0.5f;
|
||||
public float skew = 0.0f;
|
||||
public float radius = 0.0f;
|
||||
public float revolutions = 1.0f;
|
||||
|
||||
public float pathCutBegin = 0.0f;
|
||||
public float pathCutEnd = 1.0f;
|
||||
|
||||
public ushort pathBegin = 0;
|
||||
public ushort pathEnd = 0;
|
||||
|
||||
public float pathTaperX = 0.0f;
|
||||
public float pathTaperY = 0.0f;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an extrusion of a profile along a linear path. Used to create prim types box, cylinder, and prism.
|
||||
/// </summary>
|
||||
/// <param name="m"></param>
|
||||
/// <returns>A mesh of the extruded shape</returns>
|
||||
public Mesh ExtrudeLinearPath(Mesh m)
|
||||
{
|
||||
Mesh result = new Mesh();
|
||||
|
||||
Mesh newLayer;
|
||||
Mesh lastLayer = null;
|
||||
|
||||
int step = 0;
|
||||
int steps = 1;
|
||||
|
||||
float twistTotal = twistTop - twistBot;
|
||||
// if the profile has a lot of twist, add more layers otherwise the layers may overlap
|
||||
// and the resulting mesh may be quite inaccurate. This method is arbitrary and may not
|
||||
// accurately match the viewer
|
||||
float twistTotalAbs = System.Math.Abs(twistTotal);
|
||||
if (twistTotalAbs > 0.01)
|
||||
steps += (int)(twistTotalAbs * 3.66f); // dahlia's magic number ;)
|
||||
|
||||
#if SPAM
|
||||
System.Console.WriteLine("ExtrudeLinearPath: twistTotalAbs: " + twistTotalAbs.ToString() + " steps: " + steps.ToString());
|
||||
#endif
|
||||
|
||||
double percentOfPathMultiplier = 1.0 / steps;
|
||||
|
||||
float start = -0.5f;
|
||||
|
||||
float stepSize = 1.0f / (float)steps;
|
||||
|
||||
float xProfileScale = 1.0f;
|
||||
float yProfileScale = 1.0f;
|
||||
|
||||
float xOffset = 0.0f;
|
||||
float yOffset = 0.0f;
|
||||
float zOffset = start;
|
||||
|
||||
float xOffsetStepIncrement = pushX / steps;
|
||||
float yOffsetStepIncrement = pushY / steps;
|
||||
|
||||
#if SPAM
|
||||
System.Console.WriteLine("Extruder: twistTop: " + twistTop.ToString() + " twistbot: " + twistBot.ToString() + " twisttotal: " + twistTotal.ToString());
|
||||
System.Console.WriteLine("Extruder: taperBotFactorX: " + taperBotFactorX.ToString() + " taperBotFactorY: " + taperBotFactorY.ToString()
|
||||
+ " taperTopFactorX: " + taperTopFactorX.ToString() + " taperTopFactorY: " + taperTopFactorY.ToString());
|
||||
System.Console.WriteLine("Extruder: PathScaleX: " + pathScaleX.ToString() + " pathScaleY: " + pathScaleY.ToString());
|
||||
#endif
|
||||
|
||||
//float percentOfPath = 0.0f;
|
||||
float percentOfPath = (float)pathBegin * 2.0e-5f;
|
||||
zOffset += percentOfPath;
|
||||
bool done = false;
|
||||
do // loop through the length of the path and add the layers
|
||||
{
|
||||
newLayer = m.Clone();
|
||||
|
||||
if (taperBotFactorX < 1.0f)
|
||||
xProfileScale = 1.0f - (1.0f - percentOfPath) * (1.0f - taperBotFactorX);
|
||||
else if (taperTopFactorX < 1.0f)
|
||||
xProfileScale = 1.0f - percentOfPath * (1.0f - taperTopFactorX);
|
||||
else xProfileScale = 1.0f;
|
||||
|
||||
if (taperBotFactorY < 1.0f)
|
||||
yProfileScale = 1.0f - (1.0f - percentOfPath) * (1.0f - taperBotFactorY);
|
||||
else if (taperTopFactorY < 1.0f)
|
||||
yProfileScale = 1.0f - percentOfPath * (1.0f - taperTopFactorY);
|
||||
else yProfileScale = 1.0f;
|
||||
|
||||
#if SPAM
|
||||
//System.Console.WriteLine("xProfileScale: " + xProfileScale.ToString() + " yProfileScale: " + yProfileScale.ToString());
|
||||
#endif
|
||||
Vertex vTemp = new Vertex(0.0f, 0.0f, 0.0f);
|
||||
|
||||
// apply the taper to the profile before any rotations
|
||||
if (xProfileScale != 1.0f || yProfileScale != 1.0f)
|
||||
{
|
||||
foreach (Vertex v in newLayer.vertices)
|
||||
{
|
||||
if (v != null)
|
||||
{
|
||||
v.X *= xProfileScale;
|
||||
v.Y *= yProfileScale;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
float twist = twistBot + (twistTotal * (float)percentOfPath);
|
||||
#if SPAM
|
||||
System.Console.WriteLine("Extruder: percentOfPath: " + percentOfPath.ToString() + " zOffset: " + zOffset.ToString()
|
||||
+ " xProfileScale: " + xProfileScale.ToString() + " yProfileScale: " + yProfileScale.ToString());
|
||||
#endif
|
||||
|
||||
// apply twist rotation to the profile layer and position the layer in the prim
|
||||
|
||||
Quaternion profileRot = Quaternion.CreateFromAxisAngle(new Vector3(0.0f, 0.0f, 1.0f), twist);
|
||||
foreach (Vertex v in newLayer.vertices)
|
||||
{
|
||||
if (v != null)
|
||||
{
|
||||
vTemp = v * profileRot;
|
||||
v.X = vTemp.X + xOffset;
|
||||
v.Y = vTemp.Y + yOffset;
|
||||
v.Z = vTemp.Z + zOffset;
|
||||
}
|
||||
}
|
||||
|
||||
if (step == 0) // the first layer, invert normals
|
||||
{
|
||||
foreach (Triangle t in newLayer.triangles)
|
||||
{
|
||||
t.invertNormal();
|
||||
}
|
||||
}
|
||||
|
||||
result.Append(newLayer);
|
||||
|
||||
int iLastNull = 0;
|
||||
|
||||
if (lastLayer != null)
|
||||
{
|
||||
int i, count = newLayer.vertices.Count;
|
||||
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
int iNext = (i + 1);
|
||||
|
||||
if (lastLayer.vertices[i] == null) // cant make a simplex here
|
||||
{
|
||||
iLastNull = i + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (i == count - 1) // End of list
|
||||
iNext = iLastNull;
|
||||
|
||||
if (lastLayer.vertices[iNext] == null) // Null means wrap to begin of last segment
|
||||
iNext = iLastNull;
|
||||
|
||||
result.Add(new Triangle(newLayer.vertices[i], lastLayer.vertices[i], newLayer.vertices[iNext]));
|
||||
result.Add(new Triangle(newLayer.vertices[iNext], lastLayer.vertices[i], lastLayer.vertices[iNext]));
|
||||
}
|
||||
}
|
||||
}
|
||||
lastLayer = newLayer;
|
||||
|
||||
// calc the step for the next interation of the loop
|
||||
|
||||
if (step < steps)
|
||||
{
|
||||
step++;
|
||||
percentOfPath += (float)percentOfPathMultiplier;
|
||||
|
||||
xOffset += xOffsetStepIncrement;
|
||||
yOffset += yOffsetStepIncrement;
|
||||
zOffset += stepSize;
|
||||
|
||||
if (percentOfPath > 1.0f - (float)pathEnd * 2.0e-5f)
|
||||
done = true;
|
||||
}
|
||||
else done = true;
|
||||
|
||||
} while (!done); // loop until all the layers in the path are completed
|
||||
|
||||
// scale the mesh to the desired size
|
||||
float xScale = size.X;
|
||||
float yScale = size.Y;
|
||||
float zScale = size.Z;
|
||||
|
||||
foreach (Vertex v in result.vertices)
|
||||
{
|
||||
if (v != null)
|
||||
{
|
||||
v.X *= xScale;
|
||||
v.Y *= yScale;
|
||||
v.Z *= zScale;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extrudes a shape around a circular path. Used to create prim types torus, ring, and tube.
|
||||
/// </summary>
|
||||
/// <param name="m"></param>
|
||||
/// <returns>a mesh of the extruded shape</returns>
|
||||
public Mesh ExtrudeCircularPath(Mesh m)
|
||||
{
|
||||
Mesh result = new Mesh();
|
||||
|
||||
Mesh newLayer;
|
||||
Mesh lastLayer = null;
|
||||
|
||||
int step;
|
||||
int steps = 24;
|
||||
|
||||
float twistTotal = twistTop - twistBot;
|
||||
// if the profile has a lot of twist, add more layers otherwise the layers may overlap
|
||||
// and the resulting mesh may be quite inaccurate. This method is arbitrary and doesn't
|
||||
// accurately match the viewer
|
||||
if (System.Math.Abs(twistTotal) > (float)System.Math.PI * 1.5f) steps *= 2;
|
||||
if (System.Math.Abs(twistTotal) > (float)System.Math.PI * 3.0f) steps *= 2;
|
||||
|
||||
// double percentOfPathMultiplier = 1.0 / steps;
|
||||
// double angleStepMultiplier = System.Math.PI * 2.0 / steps;
|
||||
|
||||
float yPathScale = pathScaleY * 0.5f;
|
||||
float pathLength = pathCutEnd - pathCutBegin;
|
||||
float totalSkew = skew * 2.0f * pathLength;
|
||||
float skewStart = (-skew) + pathCutBegin * 2.0f * skew;
|
||||
|
||||
// It's not quite clear what pushY (Y top shear) does, but subtracting it from the start and end
|
||||
// angles appears to approximate it's effects on path cut. Likewise, adding it to the angle used
|
||||
// to calculate the sine for generating the path radius appears to approximate it's effects there
|
||||
// too, but there are some subtle differences in the radius which are noticeable as the prim size
|
||||
// increases and it may affect megaprims quite a bit. The effect of the Y top shear parameter on
|
||||
// the meshes generated with this technique appear nearly identical in shape to the same prims when
|
||||
// displayed by the viewer.
|
||||
|
||||
|
||||
float startAngle = (float)(System.Math.PI * 2.0 * pathCutBegin * revolutions) - pushY * 0.9f;
|
||||
float endAngle = (float)(System.Math.PI * 2.0 * pathCutEnd * revolutions) - pushY * 0.9f;
|
||||
float stepSize = (float)0.2617993878; // 2*PI / 24 segments per revolution
|
||||
|
||||
step = (int)(startAngle / stepSize);
|
||||
float angle = startAngle;
|
||||
|
||||
float xProfileScale = 1.0f;
|
||||
float yProfileScale = 1.0f;
|
||||
|
||||
|
||||
#if SPAM
|
||||
System.Console.WriteLine("Extruder: twistTop: " + twistTop.ToString() + " twistbot: " + twistBot.ToString() + " twisttotal: " + twistTotal.ToString());
|
||||
System.Console.WriteLine("Extruder: startAngle: " + startAngle.ToString() + " endAngle: " + endAngle.ToString() + " step: " + step.ToString());
|
||||
System.Console.WriteLine("Extruder: taperBotFactorX: " + taperBotFactorX.ToString() + " taperBotFactorY: " + taperBotFactorY.ToString()
|
||||
+ " taperTopFactorX: " + taperTopFactorX.ToString() + " taperTopFactorY: " + taperTopFactorY.ToString());
|
||||
System.Console.WriteLine("Extruder: PathScaleX: " + pathScaleX.ToString() + " pathScaleY: " + pathScaleY.ToString());
|
||||
#endif
|
||||
|
||||
bool done = false;
|
||||
do // loop through the length of the path and add the layers
|
||||
{
|
||||
newLayer = m.Clone();
|
||||
|
||||
float percentOfPath = (angle - startAngle) / (endAngle - startAngle); // endAngle should always be larger than startAngle
|
||||
|
||||
if (pathTaperX > 0.001f) // can't really compare to 0.0f as the value passed is never exactly zero
|
||||
xProfileScale = 1.0f - percentOfPath * pathTaperX;
|
||||
else if (pathTaperX < -0.001f)
|
||||
xProfileScale = 1.0f + (1.0f - percentOfPath) * pathTaperX;
|
||||
else xProfileScale = 1.0f;
|
||||
|
||||
if (pathTaperY > 0.001f)
|
||||
yProfileScale = 1.0f - percentOfPath * pathTaperY;
|
||||
else if (pathTaperY < -0.001f)
|
||||
yProfileScale = 1.0f + (1.0f - percentOfPath) * pathTaperY;
|
||||
else yProfileScale = 1.0f;
|
||||
|
||||
#if SPAM
|
||||
//System.Console.WriteLine("xProfileScale: " + xProfileScale.ToString() + " yProfileScale: " + yProfileScale.ToString());
|
||||
#endif
|
||||
Vertex vTemp = new Vertex(0.0f, 0.0f, 0.0f);
|
||||
|
||||
// apply the taper to the profile before any rotations
|
||||
if (xProfileScale != 1.0f || yProfileScale != 1.0f)
|
||||
{
|
||||
foreach (Vertex v in newLayer.vertices)
|
||||
{
|
||||
if (v != null)
|
||||
{
|
||||
v.X *= xProfileScale;
|
||||
v.Y *= yProfileScale;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float radiusScale;
|
||||
|
||||
if (radius > 0.001f)
|
||||
radiusScale = 1.0f - radius * percentOfPath;
|
||||
else if (radius < 0.001f)
|
||||
radiusScale = 1.0f + radius * (1.0f - percentOfPath);
|
||||
else
|
||||
radiusScale = 1.0f;
|
||||
|
||||
#if SPAM
|
||||
System.Console.WriteLine("Extruder: angle: " + angle.ToString() + " percentOfPath: " + percentOfPath.ToString()
|
||||
+ " radius: " + radius.ToString() + " radiusScale: " + radiusScale.ToString()
|
||||
+ " xProfileScale: " + xProfileScale.ToString() + " yProfileScale: " + yProfileScale.ToString());
|
||||
#endif
|
||||
|
||||
float twist = twistBot + (twistTotal * (float)percentOfPath);
|
||||
|
||||
float xOffset;
|
||||
float yOffset;
|
||||
float zOffset;
|
||||
|
||||
xOffset = 0.5f * (skewStart + totalSkew * (float)percentOfPath);
|
||||
xOffset += (float) System.Math.Sin(angle) * pushX * 0.45f;
|
||||
yOffset = (float)(System.Math.Cos(angle) * (0.5f - yPathScale)) * radiusScale;
|
||||
zOffset = (float)(System.Math.Sin(angle + pushY * 0.9f) * (0.5f - yPathScale)) * radiusScale;
|
||||
|
||||
// next apply twist rotation to the profile layer
|
||||
if (twistTotal != 0.0f || twistBot != 0.0f)
|
||||
{
|
||||
Quaternion profileRot = new Quaternion(new Vector3(0.0f, 0.0f, 1.0f), twist);
|
||||
foreach (Vertex v in newLayer.vertices)
|
||||
{
|
||||
if (v != null)
|
||||
{
|
||||
vTemp = v * profileRot;
|
||||
v.X = vTemp.X;
|
||||
v.Y = vTemp.Y;
|
||||
v.Z = vTemp.Z;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// now orient the rotation of the profile layer relative to it's position on the path
|
||||
// adding pushY to the angle used to generate the quat appears to approximate the viewer
|
||||
Quaternion layerRot = Quaternion.CreateFromAxisAngle(new Vector3(1.0f, 0.0f, 0.0f), (float)angle + pushY * 0.9f);
|
||||
foreach (Vertex v in newLayer.vertices)
|
||||
{
|
||||
if (v != null)
|
||||
{
|
||||
vTemp = v * layerRot;
|
||||
v.X = vTemp.X + xOffset;
|
||||
v.Y = vTemp.Y + yOffset;
|
||||
v.Z = vTemp.Z + zOffset;
|
||||
}
|
||||
}
|
||||
|
||||
if (angle == startAngle) // the first layer, invert normals
|
||||
{
|
||||
foreach (Triangle t in newLayer.triangles)
|
||||
{
|
||||
t.invertNormal();
|
||||
}
|
||||
}
|
||||
|
||||
result.Append(newLayer);
|
||||
|
||||
int iLastNull = 0;
|
||||
|
||||
if (lastLayer != null)
|
||||
{
|
||||
int i, count = newLayer.vertices.Count;
|
||||
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
int iNext = (i + 1);
|
||||
|
||||
if (lastLayer.vertices[i] == null) // cant make a simplex here
|
||||
{
|
||||
iLastNull = i + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (i == count - 1) // End of list
|
||||
iNext = iLastNull;
|
||||
|
||||
if (lastLayer.vertices[iNext] == null) // Null means wrap to begin of last segment
|
||||
iNext = iLastNull;
|
||||
|
||||
result.Add(new Triangle(newLayer.vertices[i], lastLayer.vertices[i], newLayer.vertices[iNext]));
|
||||
result.Add(new Triangle(newLayer.vertices[iNext], lastLayer.vertices[i], lastLayer.vertices[iNext]));
|
||||
}
|
||||
}
|
||||
}
|
||||
lastLayer = newLayer;
|
||||
|
||||
// calc the angle for the next interation of the loop
|
||||
if (angle >= endAngle)
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
angle = stepSize * ++step;
|
||||
if (angle > endAngle)
|
||||
angle = endAngle;
|
||||
}
|
||||
} while (!done); // loop until all the layers in the path are completed
|
||||
|
||||
// scale the mesh to the desired size
|
||||
float xScale = size.X;
|
||||
float yScale = size.Y;
|
||||
float zScale = size.Z;
|
||||
|
||||
foreach (Vertex v in result.vertices)
|
||||
{
|
||||
if (v != null)
|
||||
{
|
||||
v.X *= xScale;
|
||||
v.Y *= yScale;
|
||||
v.Z *= zScale;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -356,20 +356,6 @@ public class Triangle
|
|||
radius_square = (float) (rx*rx + ry*ry);
|
||||
}
|
||||
|
||||
public List<Simplex> GetSimplices()
|
||||
{
|
||||
List<Simplex> result = new List<Simplex>();
|
||||
Simplex s1 = new Simplex(v1, v2);
|
||||
Simplex s2 = new Simplex(v2, v3);
|
||||
Simplex s3 = new Simplex(v3, v1);
|
||||
|
||||
result.Add(s1);
|
||||
result.Add(s2);
|
||||
result.Add(s3);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public override String ToString()
|
||||
{
|
||||
NumberFormatInfo nfi = new NumberFormatInfo();
|
||||
|
|
|
@ -115,26 +115,6 @@ namespace OpenSim.Region.Physics.Meshing
|
|||
vertices.Remove(v);
|
||||
}
|
||||
|
||||
public void RemoveTrianglesOutside(SimpleHull hull)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < triangles.Count; i++)
|
||||
{
|
||||
Triangle t = triangles[i];
|
||||
Vertex v1 = t.v1;
|
||||
Vertex v2 = t.v2;
|
||||
Vertex v3 = t.v3;
|
||||
PhysicsVector m = v1 + v2 + v3;
|
||||
m /= 3.0f;
|
||||
if (!hull.IsPointIn(new Vertex(m)))
|
||||
{
|
||||
triangles.RemoveAt(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(List<Vertex> lv)
|
||||
{
|
||||
foreach (Vertex v in lv)
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) Contributors, http://opensimulator.org/
|
||||
* Copyright (c) Contributors
|
||||
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -243,10 +243,11 @@ namespace PrimMesher
|
|||
public int n2;
|
||||
public int n3;
|
||||
|
||||
//// UVs
|
||||
//public int uv1;
|
||||
//public int uv2;
|
||||
//public int uv3;
|
||||
// uvs
|
||||
public int uv1;
|
||||
public int uv2;
|
||||
public int uv3;
|
||||
|
||||
|
||||
public Face(int v1, int v2, int v3)
|
||||
{
|
||||
|
@ -260,9 +261,10 @@ namespace PrimMesher
|
|||
this.n2 = 0;
|
||||
this.n3 = 0;
|
||||
|
||||
//this.uv1 = 0;
|
||||
//this.uv2 = 0;
|
||||
//this.uv3 = 0;
|
||||
this.uv1 = 0;
|
||||
this.uv2 = 0;
|
||||
this.uv3 = 0;
|
||||
|
||||
}
|
||||
|
||||
public Face(int v1, int v2, int v3, int n1, int n2, int n3)
|
||||
|
@ -277,9 +279,21 @@ namespace PrimMesher
|
|||
this.n2 = n2;
|
||||
this.n3 = n3;
|
||||
|
||||
//this.uv1 = 0;
|
||||
//this.uv2 = 0;
|
||||
//this.uv3 = 0;
|
||||
this.uv1 = 0;
|
||||
this.uv2 = 0;
|
||||
this.uv3 = 0;
|
||||
}
|
||||
|
||||
public Coord SurfaceNormal(List<Coord> coordList)
|
||||
{
|
||||
Coord c1 = coordList[this.v1];
|
||||
Coord c2 = coordList[this.v2];
|
||||
Coord c3 = coordList[this.v3];
|
||||
|
||||
Coord edge1 = new Coord(c2.X - c1.X, c2.Y - c1.Y, c2.Z - c1.Z);
|
||||
Coord edge2 = new Coord(c3.X - c1.X, c3.Y - c1.Y, c3.Z - c1.Z);
|
||||
|
||||
return Coord.Cross(edge1, edge2).Normalize();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -560,7 +574,7 @@ namespace PrimMesher
|
|||
/// <summary>
|
||||
/// generates a profile for extrusion
|
||||
/// </summary>
|
||||
public class Profile
|
||||
internal class Profile
|
||||
{
|
||||
private const float twoPi = 2.0f * (float)Math.PI;
|
||||
|
||||
|
@ -569,6 +583,7 @@ namespace PrimMesher
|
|||
internal List<Coord> vertexNormals;
|
||||
internal List<float> us;
|
||||
internal List<UVCoord> faceUVs;
|
||||
internal List<int> faceNumbers;
|
||||
|
||||
internal Coord faceNormal = new Coord(0.0f, 0.0f, 1.0f);
|
||||
internal Coord cutNormal1 = new Coord();
|
||||
|
@ -578,6 +593,8 @@ namespace PrimMesher
|
|||
internal int numHollowVerts = 0;
|
||||
|
||||
internal bool calcVertexNormals = false;
|
||||
internal int bottomFaceNumber = 0;
|
||||
internal int numPrimFaces = 0;
|
||||
|
||||
internal Profile()
|
||||
{
|
||||
|
@ -586,9 +603,10 @@ namespace PrimMesher
|
|||
this.vertexNormals = new List<Coord>();
|
||||
this.us = new List<float>();
|
||||
this.faceUVs = new List<UVCoord>();
|
||||
this.faceNumbers = new List<int>();
|
||||
}
|
||||
|
||||
public Profile(int sides, float profileStart, float profileEnd, float hollow, int hollowSides, bool createFaces, bool calcVertexNormals)
|
||||
internal Profile(int sides, float profileStart, float profileEnd, float hollow, int hollowSides, bool createFaces, bool calcVertexNormals)
|
||||
{
|
||||
this.calcVertexNormals = calcVertexNormals;
|
||||
this.coords = new List<Coord>();
|
||||
|
@ -596,6 +614,8 @@ namespace PrimMesher
|
|||
this.vertexNormals = new List<Coord>();
|
||||
this.us = new List<float>();
|
||||
this.faceUVs = new List<UVCoord>();
|
||||
this.faceNumbers = new List<int>();
|
||||
|
||||
Coord center = new Coord(0.0f, 0.0f, 0.0f);
|
||||
|
||||
List<Coord> hollowCoords = new List<Coord>();
|
||||
|
@ -674,7 +694,7 @@ namespace PrimMesher
|
|||
hollowCoords.Add(newVert);
|
||||
if (this.calcVertexNormals)
|
||||
{
|
||||
if (sides < 5)
|
||||
if (hollowSides < 5)
|
||||
hollowNormals.Add(hollowAngles.normals[i].Invert());
|
||||
else
|
||||
hollowNormals.Add(new Coord(-angle.X, -angle.Y, 0.0f));
|
||||
|
@ -689,7 +709,7 @@ namespace PrimMesher
|
|||
|
||||
for (int i = 0; i < numAngles; i++)
|
||||
{
|
||||
//int iNext = i == numAngles ? i + 1 : 0;
|
||||
int iNext = i == numAngles ? i + 1 : 0;
|
||||
angle = angles.angles[i];
|
||||
newVert.X = angle.X * xScale;
|
||||
newVert.Y = angle.Y * yScale;
|
||||
|
@ -884,21 +904,46 @@ namespace PrimMesher
|
|||
hollowNormals = null;
|
||||
hollowUs = null;
|
||||
|
||||
if (calcVertexNormals)
|
||||
{ // calculate prim face numbers
|
||||
// I know it's ugly but so is the whole concept of prim face numbers
|
||||
int faceNum = 1;
|
||||
int startVert = hasProfileCut && !hasHollow ? 1 : 0;
|
||||
if (startVert > 0)
|
||||
this.faceNumbers.Add(0);
|
||||
for (int i = 0; i < numOuterVerts; i++)
|
||||
this.faceNumbers.Add(sides < 5 ? faceNum++ : faceNum);
|
||||
if (sides > 4)
|
||||
faceNum++;
|
||||
if (hasProfileCut)
|
||||
this.faceNumbers.Add(0);
|
||||
for (int i = 0; i < numHollowVerts; i++)
|
||||
this.faceNumbers.Add(faceNum++);
|
||||
this.bottomFaceNumber = faceNum++;
|
||||
if (hasHollow && hasProfileCut)
|
||||
this.faceNumbers.Add(faceNum++);
|
||||
for (int i = 0; i < this.faceNumbers.Count; i++)
|
||||
if (this.faceNumbers[i] == 0)
|
||||
this.faceNumbers[i] = faceNum++;
|
||||
|
||||
this.numPrimFaces = faceNum;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void MakeFaceUVs()
|
||||
internal void MakeFaceUVs()
|
||||
{
|
||||
this.faceUVs = new List<UVCoord>();
|
||||
foreach (Coord c in this.coords)
|
||||
this.faceUVs.Add(new UVCoord(1.0f - (0.5f + c.X), 1.0f - (0.5f - c.Y)));
|
||||
}
|
||||
|
||||
public Profile Clone()
|
||||
internal Profile Clone()
|
||||
{
|
||||
return this.Clone(true);
|
||||
}
|
||||
|
||||
public Profile Clone(bool needFaces)
|
||||
internal Profile Clone(bool needFaces)
|
||||
{
|
||||
Profile clone = new Profile();
|
||||
|
||||
|
@ -914,6 +959,7 @@ namespace PrimMesher
|
|||
clone.cutNormal1 = this.cutNormal1;
|
||||
clone.cutNormal2 = this.cutNormal2;
|
||||
clone.us.AddRange(this.us);
|
||||
clone.faceNumbers.AddRange(this.faceNumbers);
|
||||
}
|
||||
clone.numOuterVerts = this.numOuterVerts;
|
||||
clone.numHollowVerts = this.numHollowVerts;
|
||||
|
@ -921,12 +967,12 @@ namespace PrimMesher
|
|||
return clone;
|
||||
}
|
||||
|
||||
public void AddPos(Coord v)
|
||||
internal void AddPos(Coord v)
|
||||
{
|
||||
this.AddPos(v.X, v.Y, v.Z);
|
||||
}
|
||||
|
||||
public void AddPos(float x, float y, float z)
|
||||
internal void AddPos(float x, float y, float z)
|
||||
{
|
||||
int i;
|
||||
int numVerts = this.coords.Count;
|
||||
|
@ -942,7 +988,7 @@ namespace PrimMesher
|
|||
}
|
||||
}
|
||||
|
||||
public void AddRot(Quat q)
|
||||
internal void AddRot(Quat q)
|
||||
{
|
||||
int i;
|
||||
int numVerts = this.coords.Count;
|
||||
|
@ -963,7 +1009,7 @@ namespace PrimMesher
|
|||
}
|
||||
}
|
||||
|
||||
public void Scale(float x, float y)
|
||||
internal void Scale(float x, float y)
|
||||
{
|
||||
int i;
|
||||
int numVerts = this.coords.Count;
|
||||
|
@ -981,7 +1027,7 @@ namespace PrimMesher
|
|||
/// <summary>
|
||||
/// Changes order of the vertex indices and negates the center vertex normal. Does not alter vertex normals of radial vertices
|
||||
/// </summary>
|
||||
public void FlipNormals()
|
||||
internal void FlipNormals()
|
||||
{
|
||||
int i;
|
||||
int numFaces = this.faces.Count;
|
||||
|
@ -1021,7 +1067,7 @@ namespace PrimMesher
|
|||
}
|
||||
}
|
||||
|
||||
public void AddValue2FaceVertexIndices(int num)
|
||||
internal void AddValue2FaceVertexIndices(int num)
|
||||
{
|
||||
int numFaces = this.faces.Count;
|
||||
Face tmpFace;
|
||||
|
@ -1036,7 +1082,7 @@ namespace PrimMesher
|
|||
}
|
||||
}
|
||||
|
||||
public void AddValue2FaceNormalIndices(int num)
|
||||
internal void AddValue2FaceNormalIndices(int num)
|
||||
{
|
||||
if (this.calcVertexNormals)
|
||||
{
|
||||
|
@ -1054,7 +1100,7 @@ namespace PrimMesher
|
|||
}
|
||||
}
|
||||
|
||||
public void DumpRaw(String path, String name, String title)
|
||||
internal void DumpRaw(String path, String name, String title)
|
||||
{
|
||||
if (path == null)
|
||||
return;
|
||||
|
@ -1113,6 +1159,12 @@ namespace PrimMesher
|
|||
private bool normalsProcessed = false;
|
||||
public bool viewerMode = false;
|
||||
|
||||
public int numPrimFaces = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Human readable string representation of the parameters used to create a mesh.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string ParamsToDisplayString()
|
||||
{
|
||||
string s = "";
|
||||
|
@ -1141,7 +1193,14 @@ namespace PrimMesher
|
|||
return s;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a PrimMesh object and creates the profile for extrusion.
|
||||
/// </summary>
|
||||
/// <param name="sides"></param>
|
||||
/// <param name="profileStart"></param>
|
||||
/// <param name="profileEnd"></param>
|
||||
/// <param name="hollow"></param>
|
||||
/// <param name="hollowSides"></param>
|
||||
public PrimMesh(int sides, float profileStart, float profileEnd, float hollow, int hollowSides)
|
||||
{
|
||||
this.coords = new List<Coord>();
|
||||
|
@ -1174,6 +1233,9 @@ namespace PrimMesher
|
|||
this.hasHollow = (this.hollow > 0.001f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extrudes a profile along a straight line path. Used for prim types box, cylinder, and prism.
|
||||
/// </summary>
|
||||
public void ExtrudeLinear()
|
||||
{
|
||||
this.coords = new List<Coord>();
|
||||
|
@ -1248,6 +1310,7 @@ namespace PrimMesher
|
|||
hollow *= 1.414f;
|
||||
|
||||
Profile profile = new Profile(this.sides, this.profileStart, this.profileEnd, hollow, this.hollowSides, true, calcVertexNormals);
|
||||
this.numPrimFaces = profile.numPrimFaces;
|
||||
|
||||
int cut1Vert = -1;
|
||||
int cut2Vert = -1;
|
||||
|
@ -1398,7 +1461,7 @@ namespace PrimMesher
|
|||
if (u2 < 0.1f)
|
||||
u2 = 1.0f;
|
||||
|
||||
newViewerFace2.primFaceNumber = newViewerFace1.primFaceNumber = whichVert + 1;
|
||||
//newViewerFace2.primFaceNumber = newViewerFace1.primFaceNumber = whichVert + 1;
|
||||
}
|
||||
|
||||
newViewerFace1.uv1.U = u1;
|
||||
|
@ -1462,6 +1525,8 @@ namespace PrimMesher
|
|||
}
|
||||
}
|
||||
|
||||
newViewerFace2.primFaceNumber = newViewerFace1.primFaceNumber = newLayer.faceNumbers[whichVert];
|
||||
|
||||
this.viewerFaces.Add(newViewerFace1);
|
||||
this.viewerFaces.Add(newViewerFace2);
|
||||
|
||||
|
@ -1492,7 +1557,7 @@ namespace PrimMesher
|
|||
// add the top faces to the viewerFaces list here
|
||||
Coord faceNormal = newLayer.faceNormal;
|
||||
ViewerFace newViewerFace = new ViewerFace();
|
||||
newViewerFace.primFaceNumber = 0;
|
||||
newViewerFace.primFaceNumber = newLayer.bottomFaceNumber;
|
||||
foreach (Face face in newLayer.faces)
|
||||
{
|
||||
newViewerFace.v1 = newLayer.coords[face.v1 - coordsLen];
|
||||
|
@ -1513,6 +1578,9 @@ namespace PrimMesher
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extrude a profile into a circular path prim mesh. Used for prim types torus, tube, and ring.
|
||||
/// </summary>
|
||||
public void ExtrudeCircular()
|
||||
{
|
||||
this.coords = new List<Coord>();
|
||||
|
@ -1615,6 +1683,7 @@ namespace PrimMesher
|
|||
needEndFaces = true;
|
||||
|
||||
Profile profile = new Profile(this.sides, this.profileStart, this.profileEnd, hollow, this.hollowSides, needEndFaces, calcVertexNormals);
|
||||
this.numPrimFaces = profile.numPrimFaces;
|
||||
|
||||
int cut1Vert = -1;
|
||||
int cut2Vert = -1;
|
||||
|
@ -1787,7 +1856,7 @@ namespace PrimMesher
|
|||
if (u2 < 0.1f)
|
||||
u2 = 1.0f;
|
||||
|
||||
newViewerFace2.primFaceNumber = newViewerFace1.primFaceNumber = whichVert + 1;
|
||||
//newViewerFace2.primFaceNumber = newViewerFace1.primFaceNumber = whichVert + 1;
|
||||
}
|
||||
|
||||
newViewerFace1.uv1.U = u1;
|
||||
|
@ -1865,6 +1934,7 @@ namespace PrimMesher
|
|||
}
|
||||
}
|
||||
|
||||
newViewerFace1.primFaceNumber = newViewerFace2.primFaceNumber = newLayer.faceNumbers[whichVert];
|
||||
this.viewerFaces.Add(newViewerFace1);
|
||||
this.viewerFaces.Add(newViewerFace2);
|
||||
|
||||
|
@ -1894,7 +1964,7 @@ namespace PrimMesher
|
|||
// add the bottom faces to the viewerFaces list here
|
||||
Coord faceNormal = newLayer.faceNormal;
|
||||
ViewerFace newViewerFace = new ViewerFace();
|
||||
newViewerFace.primFaceNumber = 0;
|
||||
newViewerFace.primFaceNumber = newLayer.bottomFaceNumber;
|
||||
foreach (Face face in newLayer.faces)
|
||||
{
|
||||
newViewerFace.v1 = newLayer.coords[face.v1 - coordsLen];
|
||||
|
@ -1932,6 +2002,11 @@ namespace PrimMesher
|
|||
return SurfaceNormal(this.coords[face.v1], this.coords[face.v2], this.coords[face.v3]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculate the surface normal for a face in the list of faces
|
||||
/// </summary>
|
||||
/// <param name="faceIndex"></param>
|
||||
/// <returns></returns>
|
||||
public Coord SurfaceNormal(int faceIndex)
|
||||
{
|
||||
int numFaces = this.faces.Count;
|
||||
|
@ -1941,6 +2016,9 @@ namespace PrimMesher
|
|||
return SurfaceNormal(this.faces[faceIndex]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculate surface normals for all of the faces in the list of faces in this mesh
|
||||
/// </summary>
|
||||
public void CalcNormals()
|
||||
{
|
||||
if (normalsProcessed)
|
||||
|
@ -1968,6 +2046,12 @@ namespace PrimMesher
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a value to each XYZ vertex coordinate in the mesh
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <param name="z"></param>
|
||||
public void AddPos(float x, float y, float z)
|
||||
{
|
||||
int i;
|
||||
|
@ -1984,9 +2068,12 @@ namespace PrimMesher
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rotates the mesh
|
||||
/// </summary>
|
||||
/// <param name="q"></param>
|
||||
public void AddRot(Quat q)
|
||||
{
|
||||
Console.WriteLine("AddRot(" + q.ToString() + ")");
|
||||
int i;
|
||||
int numVerts = this.coords.Count;
|
||||
|
||||
|
@ -2020,6 +2107,12 @@ namespace PrimMesher
|
|||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scales the mesh
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <param name="z"></param>
|
||||
public void Scale(float x, float y, float z)
|
||||
{
|
||||
int i;
|
||||
|
@ -2046,6 +2139,12 @@ namespace PrimMesher
|
|||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dumps the mesh to a Blender compatible "Raw" format file
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="title"></param>
|
||||
public void DumpRaw(String path, String name, String title)
|
||||
{
|
||||
if (path == null)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) Contributors, http://opensimulator.org/
|
||||
* Copyright (c) Contributors
|
||||
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -27,314 +27,317 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Text;
|
||||
using OpenMetaverse.Imaging;
|
||||
|
||||
namespace OpenSim.Region.Physics.Meshing
|
||||
namespace PrimMesher
|
||||
{
|
||||
// This functionality based on the XNA SculptPreview by John Hurliman.
|
||||
public class SculptMesh : Mesh
|
||||
|
||||
public class SculptMesh
|
||||
{
|
||||
Image idata = null;
|
||||
Bitmap bLOD = null;
|
||||
Bitmap bBitmap = null;
|
||||
public List<Coord> coords;
|
||||
public List<Face> faces;
|
||||
|
||||
Vertex northpole = new Vertex(0, 0, 0);
|
||||
Vertex southpole = new Vertex(0, 0, 0);
|
||||
public List<ViewerFace> viewerFaces;
|
||||
public List<Coord> normals;
|
||||
public List<UVCoord> uvs;
|
||||
|
||||
private int lod = 32;
|
||||
private const float RANGE = 128.0f;
|
||||
public enum SculptType { sphere = 1, torus = 2, plane = 3, cylinder = 4 };
|
||||
private const float pixScale = 0.00390625f; // 1.0 / 256
|
||||
|
||||
public SculptMesh(byte[] jpegData, float _lod)
|
||||
private Bitmap ScaleImage(Bitmap srcImage, float scale)
|
||||
{
|
||||
if (_lod == 2f || _lod == 4f || _lod == 8f || _lod == 16f || _lod == 32f || _lod == 64f)
|
||||
lod = (int)_lod;
|
||||
int sourceWidth = srcImage.Width;
|
||||
int sourceHeight = srcImage.Height;
|
||||
int sourceX = 0;
|
||||
int sourceY = 0;
|
||||
|
||||
try
|
||||
{
|
||||
ManagedImage managedImage; // we never use this
|
||||
OpenJPEG.DecodeToImage(jpegData, out managedImage, out idata);
|
||||
//int i = 0;
|
||||
//i = i / i;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
System.Console.WriteLine("[PHYSICS]: Unable to generate a Sculpty physics proxy. Sculpty texture decode failed!");
|
||||
return;
|
||||
}
|
||||
int destX = 0;
|
||||
int destY = 0;
|
||||
int destWidth = (int)(sourceWidth * scale);
|
||||
int destHeight = (int)(sourceHeight * scale);
|
||||
|
||||
if (idata != null)
|
||||
{
|
||||
bBitmap = new Bitmap(idata);
|
||||
if (bBitmap.Width == bBitmap.Height)
|
||||
Bitmap scaledImage = new Bitmap(destWidth, destHeight,
|
||||
PixelFormat.Format24bppRgb);
|
||||
scaledImage.SetResolution(srcImage.HorizontalResolution,
|
||||
srcImage.VerticalResolution);
|
||||
|
||||
Graphics grPhoto = Graphics.FromImage(scaledImage);
|
||||
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
|
||||
|
||||
grPhoto.DrawImage(srcImage,
|
||||
new Rectangle(destX, destY, destWidth, destHeight),
|
||||
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
|
||||
GraphicsUnit.Pixel);
|
||||
|
||||
grPhoto.Dispose();
|
||||
return scaledImage;
|
||||
}
|
||||
|
||||
public SculptMesh SculptMeshFromFile(string fileName, SculptType sculptType, int lod, bool viewerMode)
|
||||
{
|
||||
Bitmap bitmap = (Bitmap)Bitmap.FromFile(fileName);
|
||||
SculptMesh sculptMesh = new SculptMesh(bitmap, sculptType, lod, viewerMode);
|
||||
bitmap.Dispose();
|
||||
return sculptMesh;
|
||||
}
|
||||
|
||||
public SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool viewerMode)
|
||||
{
|
||||
coords = new List<Coord>();
|
||||
faces = new List<Face>();
|
||||
normals = new List<Coord>();
|
||||
uvs = new List<UVCoord>();
|
||||
|
||||
float sourceScaleFactor = (float)lod / (float)Math.Max(sculptBitmap.Width, sculptBitmap.Height);
|
||||
bool scaleSourceImage = sourceScaleFactor < 1.0f ? true : false;
|
||||
|
||||
Bitmap bitmap;
|
||||
if (scaleSourceImage)
|
||||
bitmap = ScaleImage(sculptBitmap, sourceScaleFactor);
|
||||
else
|
||||
bitmap = sculptBitmap;
|
||||
|
||||
viewerFaces = new List<ViewerFace>();
|
||||
|
||||
int width = bitmap.Width;
|
||||
int height = bitmap.Height;
|
||||
|
||||
float widthUnit = 1.0f / width;
|
||||
float heightUnit = 1.0f / (height - 1);
|
||||
|
||||
int p1, p2, p3, p4;
|
||||
Color color;
|
||||
float x, y, z;
|
||||
|
||||
int imageX, imageY;
|
||||
|
||||
if (sculptType == SculptType.sphere)
|
||||
{ // average the top and bottom row pixel values so the resulting vertices appear to converge
|
||||
int lastRow = height - 1;
|
||||
int r1 = 0, g1 = 0, b1 = 0;
|
||||
int r2 = 0, g2 = 0, b2 = 0;
|
||||
for (imageX = 0; imageX < width; imageX++)
|
||||
{
|
||||
DoLOD();
|
||||
Color c1 = bitmap.GetPixel(imageX, 0);
|
||||
Color c2 = bitmap.GetPixel(imageX, lastRow);
|
||||
|
||||
LoadPoles();
|
||||
r1 += c1.R;
|
||||
g1 += c1.G;
|
||||
b1 += c1.B;
|
||||
|
||||
processSculptTexture();
|
||||
r2 += c2.R;
|
||||
g2 += c2.G;
|
||||
b2 += c2.B;
|
||||
}
|
||||
|
||||
bLOD.Dispose();
|
||||
bBitmap.Dispose();
|
||||
idata.Dispose();
|
||||
Color newC1 = Color.FromArgb(r1 / width, g1 / width, b1 / width);
|
||||
Color newC2 = Color.FromArgb(r2 / width, g2 / width, b2 / width);
|
||||
|
||||
for (imageX = 0; imageX < width; imageX++)
|
||||
{
|
||||
bitmap.SetPixel(imageX, 0, newC1);
|
||||
bitmap.SetPixel(imageX, lastRow, newC2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Vertex ColorToVertex(Color input)
|
||||
{
|
||||
return new Vertex(
|
||||
((float)input.R - 128) / RANGE,
|
||||
((float)input.G - 128) / RANGE,
|
||||
((float)input.B - 128) / RANGE);
|
||||
}
|
||||
|
||||
private void LoadPoles()
|
||||
{
|
||||
northpole = new Vertex(0, 0, 0);
|
||||
for (int x = 0; x < bLOD.Width; x++)
|
||||
int pixelsAcross = sculptType == SculptType.plane ? width : width + 1;
|
||||
int pixelsDown = sculptType == SculptType.sphere || sculptType == SculptType.cylinder ? height + 1 : height;
|
||||
|
||||
for (imageY = 0; imageY < pixelsDown; imageY++)
|
||||
{
|
||||
northpole += ColorToVertex(GetPixel(0, 0));
|
||||
}
|
||||
northpole /= bLOD.Width;
|
||||
int rowOffset = imageY * width;
|
||||
|
||||
southpole = new Vertex(0, 0, 0);
|
||||
for (int x = 0; x < bLOD.Width; x++)
|
||||
{
|
||||
//System.Console.WriteLine("Height: " + bLOD.Height.ToString());
|
||||
southpole += ColorToVertex(GetPixel(bLOD.Height - 1, (bLOD.Height - 1)));
|
||||
}
|
||||
southpole /= bBitmap.Width;
|
||||
}
|
||||
|
||||
private Color GetPixel(int x, int y)
|
||||
{
|
||||
return bLOD.GetPixel(x, y);
|
||||
}
|
||||
|
||||
public int LOD
|
||||
{
|
||||
get
|
||||
{
|
||||
return (int)Math.Log(Scale, 2);
|
||||
}
|
||||
set
|
||||
{
|
||||
int power = value;
|
||||
if (power == 0)
|
||||
power = 6;
|
||||
if (power < 2)
|
||||
power = 2;
|
||||
if (power > 9)
|
||||
power = 9;
|
||||
int t = (int)Math.Pow(2, power);
|
||||
if (t != Scale)
|
||||
for (imageX = 0; imageX < pixelsAcross; imageX++)
|
||||
{
|
||||
lod = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* p1-----p2
|
||||
* | \ f2 |
|
||||
* | \ |
|
||||
* | f1 \|
|
||||
* p3-----p4
|
||||
*/
|
||||
|
||||
public int Scale
|
||||
{
|
||||
get
|
||||
{
|
||||
return lod;
|
||||
}
|
||||
}
|
||||
|
||||
private void DoLOD()
|
||||
{
|
||||
int x_max = Math.Min(Scale, bBitmap.Width);
|
||||
int y_max = Math.Min(Scale, bBitmap.Height);
|
||||
if (bBitmap.Width == x_max && bBitmap.Height == y_max)
|
||||
bLOD = bBitmap;
|
||||
|
||||
else if (bLOD == null || x_max != bLOD.Width || y_max != bLOD.Height)//don't resize if you don't need to.
|
||||
{
|
||||
System.Drawing.Bitmap tile = new System.Drawing.Bitmap(bBitmap.Width * 2, bBitmap.Height, PixelFormat.Format24bppRgb);
|
||||
System.Drawing.Bitmap tile_LOD = new System.Drawing.Bitmap(x_max * 2, y_max, PixelFormat.Format24bppRgb);
|
||||
|
||||
bLOD = new System.Drawing.Bitmap(x_max, y_max, PixelFormat.Format24bppRgb);
|
||||
bLOD.SetResolution(bBitmap.HorizontalResolution, bBitmap.VerticalResolution);
|
||||
|
||||
System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(tile);
|
||||
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
|
||||
|
||||
grPhoto.DrawImage(bBitmap,
|
||||
new System.Drawing.Rectangle(0, 0, bBitmap.Width / 2, bBitmap.Height),
|
||||
new System.Drawing.Rectangle(bBitmap.Width / 2, 0, bBitmap.Width / 2, bBitmap.Height),
|
||||
System.Drawing.GraphicsUnit.Pixel);
|
||||
|
||||
grPhoto.DrawImage(bBitmap,
|
||||
new System.Drawing.Rectangle((3 * bBitmap.Width) / 2, 0, bBitmap.Width / 2, bBitmap.Height),
|
||||
new System.Drawing.Rectangle(0, 0, bBitmap.Width / 2, bBitmap.Height),
|
||||
System.Drawing.GraphicsUnit.Pixel);
|
||||
|
||||
grPhoto.DrawImage(bBitmap,
|
||||
new System.Drawing.Rectangle(bBitmap.Width / 2, 0, bBitmap.Width, bBitmap.Height),
|
||||
new System.Drawing.Rectangle(0, 0, bBitmap.Width, bBitmap.Height),
|
||||
System.Drawing.GraphicsUnit.Pixel);
|
||||
|
||||
grPhoto = System.Drawing.Graphics.FromImage(tile_LOD);
|
||||
//grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
|
||||
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
|
||||
|
||||
grPhoto.DrawImage(tile,
|
||||
new System.Drawing.Rectangle(0, 0, tile_LOD.Width, tile_LOD.Height),
|
||||
new System.Drawing.Rectangle(0, 0, tile.Width, tile.Height),
|
||||
System.Drawing.GraphicsUnit.Pixel);
|
||||
|
||||
grPhoto = System.Drawing.Graphics.FromImage(bLOD);
|
||||
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
|
||||
|
||||
grPhoto.DrawImage(tile_LOD,
|
||||
new System.Drawing.Rectangle(0, 0, bLOD.Width, bLOD.Height),
|
||||
new System.Drawing.Rectangle(tile_LOD.Width / 4, 0, tile_LOD.Width / 2, tile_LOD.Height),
|
||||
System.Drawing.GraphicsUnit.Pixel);
|
||||
|
||||
grPhoto.Dispose();
|
||||
tile_LOD.Dispose();
|
||||
tile.Dispose();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void clearStuff()
|
||||
{
|
||||
this.triangles.Clear();
|
||||
this.vertices.Clear();
|
||||
//normals = new float[0];
|
||||
}
|
||||
|
||||
public void processSculptTexture()
|
||||
{
|
||||
int x_max = Math.Min(Scale, bBitmap.Width);
|
||||
int y_max = Math.Min(Scale, bBitmap.Height);
|
||||
|
||||
int COLUMNS = x_max + 1;
|
||||
|
||||
Vertex[] sVertices = new Vertex[COLUMNS * y_max];
|
||||
//float[] indices = new float[COLUMNS * (y_max - 1) * 6];
|
||||
|
||||
for (int y = 0; y < y_max; y++)
|
||||
{
|
||||
for (int x = 0; x < x_max; x++)
|
||||
{
|
||||
// Create the vertex
|
||||
Vertex v1 = new Vertex(0,0,0);
|
||||
|
||||
// Create a vertex position from the RGB channels in the current pixel
|
||||
// int ypos = y * bLOD.Width;
|
||||
|
||||
|
||||
if (y == 0)
|
||||
if (imageX < width)
|
||||
{
|
||||
v1 = northpole;
|
||||
}
|
||||
else if (y == y_max - 1)
|
||||
{
|
||||
v1 = southpole;
|
||||
p4 = rowOffset + imageX;
|
||||
p3 = p4 - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
v1 = ColorToVertex(GetPixel(x, y));
|
||||
p4 = rowOffset; // wrap around to beginning
|
||||
p3 = rowOffset + imageX - 1;
|
||||
}
|
||||
|
||||
// Add the vertex for use later
|
||||
if (!vertices.Contains(v1))
|
||||
Add(v1);
|
||||
p2 = p4 - width;
|
||||
p1 = p3 - width;
|
||||
|
||||
sVertices[y * COLUMNS + x] = v1;
|
||||
//System.Console.WriteLine("adding: " + v1.ToString());
|
||||
}
|
||||
//Vertex tempVertex = vertices[y * COLUMNS];
|
||||
// sVertices[y * COLUMNS + x_max] = tempVertex;
|
||||
}
|
||||
color = bitmap.GetPixel(imageX == width ? 0 : imageX, imageY == height ? height - 1 : imageY);
|
||||
|
||||
// Create the Triangles
|
||||
//int i = 0;
|
||||
x = (color.R - 128) * pixScale;
|
||||
y = (color.G - 128) * pixScale;
|
||||
z = (color.B - 128) * pixScale;
|
||||
|
||||
for (int y = 0; y < y_max - 1; y++)
|
||||
{
|
||||
int x;
|
||||
|
||||
for (x = 0; x < x_max; x++)
|
||||
{
|
||||
Vertex vt11 = sVertices[(y * COLUMNS + x)];
|
||||
Vertex vt12 = sVertices[(y * COLUMNS + (x + 1))];
|
||||
Vertex vt13 = sVertices[((y + 1) * COLUMNS + (x + 1))];
|
||||
if (vt11 != null && vt12 != null && vt13 != null)
|
||||
Coord c = new Coord(x, y, z);
|
||||
this.coords.Add(c);
|
||||
if (viewerMode)
|
||||
{
|
||||
if (vt11 != vt12 && vt11 != vt13 && vt12 != vt13)
|
||||
{
|
||||
Triangle tri1 = new Triangle(vt11, vt12, vt13);
|
||||
//indices[i++] = (ushort)(y * COLUMNS + x);
|
||||
//indices[i++] = (ushort)(y * COLUMNS + (x + 1));
|
||||
//indices[i++] = (ushort)((y + 1) * COLUMNS + (x + 1));
|
||||
Add(tri1);
|
||||
}
|
||||
this.normals.Add(new Coord());
|
||||
this.uvs.Add(new UVCoord(widthUnit * imageX, heightUnit * imageY));
|
||||
}
|
||||
|
||||
Vertex vt21 = sVertices[(y * COLUMNS + x)];
|
||||
Vertex vt22 = sVertices[((y + 1) * COLUMNS + (x + 1))];
|
||||
Vertex vt23 = sVertices[((y + 1) * COLUMNS + x)];
|
||||
if (vt21 != null && vt22 != null && vt23 != null)
|
||||
if (imageY > 0 && imageX > 0)
|
||||
{
|
||||
if (vt21.Equals(vt22, 0.022f) || vt21.Equals(vt23, 0.022f) || vt22.Equals(vt23, 0.022f))
|
||||
Face f1, f2;
|
||||
|
||||
if (viewerMode)
|
||||
{
|
||||
f1 = new Face(p1, p3, p4, p1, p3, p4);
|
||||
f1.uv1 = p1;
|
||||
f1.uv2 = p3;
|
||||
f1.uv3 = p4;
|
||||
|
||||
f2 = new Face(p1, p4, p2, p1, p4, p2);
|
||||
f2.uv1 = p1;
|
||||
f2.uv2 = p4;
|
||||
f2.uv3 = p2;
|
||||
}
|
||||
else
|
||||
{
|
||||
Triangle tri2 = new Triangle(vt21, vt22, vt23);
|
||||
//indices[i++] = (ushort)(y * COLUMNS + x);
|
||||
//indices[i++] = (ushort)((y + 1) * COLUMNS + (x + 1));
|
||||
//indices[i++] = (ushort)((y + 1) * COLUMNS + x);
|
||||
Add(tri2);
|
||||
f1 = new Face(p1, p3, p4);
|
||||
f2 = new Face(p1, p4, p2);
|
||||
}
|
||||
|
||||
this.faces.Add(f1);
|
||||
this.faces.Add(f2);
|
||||
}
|
||||
|
||||
}
|
||||
//Vertex vt31 = sVertices[(y * x_max + x)];
|
||||
//Vertex vt32 = sVertices[(y * x_max + 0)];
|
||||
//Vertex vt33 = sVertices[((y + 1) * x_max + 0)];
|
||||
//if (vt31 != null && vt32 != null && vt33 != null)
|
||||
//{
|
||||
//if (vt31.Equals(vt32, 0.022f) || vt31.Equals(vt33, 0.022f) || vt32.Equals(vt33, 0.022f))
|
||||
//{
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
//Triangle tri3 = new Triangle(vt31, vt32, vt33);
|
||||
// Wrap the last cell in the row around
|
||||
//indices[i++] = (ushort)(y * x_max + x); //a
|
||||
//indices[i++] = (ushort)(y * x_max + 0); //b
|
||||
//indices[i++] = (ushort)((y + 1) * x_max + 0); //c
|
||||
//Add(tri3);
|
||||
// }
|
||||
//}
|
||||
|
||||
//Vertex vt41 = sVertices[(y * x_max + x)];
|
||||
//Vertex vt42 = sVertices[((y + 1) * x_max + 0)];
|
||||
//Vertex vt43 = sVertices[((y + 1) * x_max + x)];
|
||||
//if (vt41 != null && vt42 != null && vt43 != null)
|
||||
//{
|
||||
//if (vt41.Equals(vt42, 0.022f) || vt31.Equals(vt43, 0.022f) || vt32.Equals(vt43, 0.022f))
|
||||
//{
|
||||
//}
|
||||
// else
|
||||
// {
|
||||
//Triangle tri4 = new Triangle(vt41, vt42, vt43);
|
||||
//indices[i++] = (ushort)(y * x_max + x); //a
|
||||
//indices[i++] = (ushort)((y + 1) * x_max + 0); //b
|
||||
//indices[i++] = (ushort)((y + 1) * x_max + x); //c
|
||||
//Add(tri4);
|
||||
//}
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
if (scaleSourceImage)
|
||||
bitmap.Dispose();
|
||||
|
||||
if (viewerMode)
|
||||
{ // compute vertex normals by summing all the surface normals of all the triangles sharing
|
||||
// each vertex and then normalizing
|
||||
int numFaces = this.faces.Count;
|
||||
for (int i = 0; i < numFaces; i++)
|
||||
{
|
||||
Face face = this.faces[i];
|
||||
Coord surfaceNormal = face.SurfaceNormal(this.coords);
|
||||
this.normals[face.v1] += surfaceNormal;
|
||||
this.normals[face.v2] += surfaceNormal;
|
||||
this.normals[face.v3] += surfaceNormal;
|
||||
}
|
||||
|
||||
int numCoords = this.coords.Count;
|
||||
for (int i = 0; i < numCoords; i++)
|
||||
this.coords[i].Normalize();
|
||||
|
||||
if (sculptType != SculptType.plane)
|
||||
{ // blend the vertex normals at the cylinder seam
|
||||
pixelsAcross = width + 1;
|
||||
for (imageY = 0; imageY < height; imageY++)
|
||||
{
|
||||
int rowOffset = imageY * pixelsAcross;
|
||||
|
||||
this.normals[rowOffset] = this.normals[rowOffset + width - 1] = (this.normals[rowOffset] + this.normals[rowOffset + width - 1]).Normalize();
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Face face in this.faces)
|
||||
{
|
||||
ViewerFace vf = new ViewerFace(0);
|
||||
vf.v1 = this.coords[face.v1];
|
||||
vf.v2 = this.coords[face.v2];
|
||||
vf.v3 = this.coords[face.v3];
|
||||
|
||||
vf.n1 = this.normals[face.n1];
|
||||
vf.n2 = this.normals[face.n2];
|
||||
vf.n3 = this.normals[face.n3];
|
||||
|
||||
vf.uv1 = this.uvs[face.uv1];
|
||||
vf.uv2 = this.uvs[face.uv2];
|
||||
vf.uv3 = this.uvs[face.uv3];
|
||||
|
||||
this.viewerFaces.Add(vf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddRot(Quat q)
|
||||
{
|
||||
int i;
|
||||
int numVerts = this.coords.Count;
|
||||
|
||||
for (i = 0; i < numVerts; i++)
|
||||
this.coords[i] *= q;
|
||||
|
||||
if (this.viewerFaces != null)
|
||||
{
|
||||
int numViewerFaces = this.viewerFaces.Count;
|
||||
|
||||
for (i = 0; i < numViewerFaces; i++)
|
||||
{
|
||||
ViewerFace v = this.viewerFaces[i];
|
||||
v.v1 *= q;
|
||||
v.v2 *= q;
|
||||
v.v3 *= q;
|
||||
|
||||
v.n1 *= q;
|
||||
v.n2 *= q;
|
||||
v.n3 *= q;
|
||||
|
||||
this.viewerFaces[i] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Scale(float x, float y, float z)
|
||||
{
|
||||
int i;
|
||||
int numVerts = this.coords.Count;
|
||||
//Coord vert;
|
||||
|
||||
Coord m = new Coord(x, y, z);
|
||||
for (i = 0; i < numVerts; i++)
|
||||
this.coords[i] *= m;
|
||||
|
||||
if (this.viewerFaces != null)
|
||||
{
|
||||
int numViewerFaces = this.viewerFaces.Count;
|
||||
for (i = 0; i < numViewerFaces; i++)
|
||||
{
|
||||
ViewerFace v = this.viewerFaces[i];
|
||||
v.v1 *= m;
|
||||
v.v2 *= m;
|
||||
v.v3 *= m;
|
||||
this.viewerFaces[i] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DumpRaw(String path, String name, String title)
|
||||
{
|
||||
if (path == null)
|
||||
return;
|
||||
String fileName = name + "_" + title + ".raw";
|
||||
String completePath = Path.Combine(path, fileName);
|
||||
StreamWriter sw = new StreamWriter(completePath);
|
||||
|
||||
for (int i = 0; i < this.faces.Count; i++)
|
||||
{
|
||||
string s = this.coords[this.faces[i].v1].ToString();
|
||||
s += " " + this.coords[this.faces[i].v2].ToString();
|
||||
s += " " + this.coords[this.faces[i].v3].ToString();
|
||||
|
||||
sw.WriteLine(s);
|
||||
}
|
||||
|
||||
sw.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,394 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) Contributors, http://opensimulator.org/
|
||||
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of the OpenSim Project nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using OpenSim.Region.Physics.Manager;
|
||||
|
||||
namespace OpenSim.Region.Physics.Meshing
|
||||
{
|
||||
// A simple hull is a set of vertices building up to simplices that border a region
|
||||
// The word simple referes to the fact, that this class assumes, that all simplices
|
||||
// do not intersect
|
||||
// Simple hulls can be added and subtracted.
|
||||
// Vertices can be checked to lie inside a hull
|
||||
// Also note, that the sequence of the vertices is important and defines if the region that
|
||||
// is defined by the hull lies inside or outside the simplex chain
|
||||
public class SimpleHull
|
||||
{
|
||||
//private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
||||
|
||||
private List<Vertex> vertices = new List<Vertex>();
|
||||
private List<Vertex> holeVertices = new List<Vertex>(); // Only used, when the hull is hollow
|
||||
|
||||
// Adds a vertex to the end of the list
|
||||
public void AddVertex(Vertex v)
|
||||
{
|
||||
vertices.Add(v);
|
||||
}
|
||||
|
||||
public override String ToString()
|
||||
{
|
||||
String result = String.Empty;
|
||||
foreach (Vertex v in vertices)
|
||||
{
|
||||
result += "b:" + v.ToString() + "\n";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public List<Vertex> getVertices()
|
||||
{
|
||||
List<Vertex> newVertices = new List<Vertex>();
|
||||
|
||||
newVertices.AddRange(vertices);
|
||||
newVertices.Add(null);
|
||||
newVertices.AddRange(holeVertices);
|
||||
|
||||
return newVertices;
|
||||
}
|
||||
|
||||
public SimpleHull Clone()
|
||||
{
|
||||
SimpleHull result = new SimpleHull();
|
||||
foreach (Vertex v in vertices)
|
||||
{
|
||||
result.AddVertex(v.Clone());
|
||||
}
|
||||
|
||||
foreach (Vertex v in holeVertices)
|
||||
{
|
||||
result.holeVertices.Add(v.Clone());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool IsPointIn(Vertex v1)
|
||||
{
|
||||
int iCounter = 0;
|
||||
List<Simplex> simplices = buildSimplexList();
|
||||
foreach (Simplex s in simplices)
|
||||
{
|
||||
// Send a ray along the positive X-Direction
|
||||
// Note, that this direction must correlate with the "below" interpretation
|
||||
// of handling for the special cases below
|
||||
PhysicsVector intersection = s.RayIntersect(v1, new PhysicsVector(1.0f, 0.0f, 0.0f), true);
|
||||
|
||||
if (intersection == null)
|
||||
continue; // No intersection. Done. More tests to follow otherwise
|
||||
|
||||
// Did we hit the end of a simplex?
|
||||
// Then this can be one of two special cases:
|
||||
// 1. we go through a border exactly at a joint
|
||||
// 2. we have just marginally touched a corner
|
||||
// 3. we can slide along a border
|
||||
// Solution: If the other vertex is "below" the ray, we don't count it
|
||||
// Thus corners pointing down are counted twice, corners pointing up are not counted
|
||||
// borders are counted once
|
||||
if (intersection.IsIdentical(s.v1, 0.001f))
|
||||
{
|
||||
if (s.v2.Y < v1.Y)
|
||||
continue;
|
||||
}
|
||||
// Do this for the other vertex two
|
||||
if (intersection.IsIdentical(s.v2, 0.001f))
|
||||
{
|
||||
if (s.v1.Y < v1.Y)
|
||||
continue;
|
||||
}
|
||||
iCounter++;
|
||||
}
|
||||
|
||||
return iCounter%2 == 1; // Point is inside if the number of intersections is odd
|
||||
}
|
||||
|
||||
public bool containsPointsFrom(SimpleHull otherHull)
|
||||
{
|
||||
foreach (Vertex v in otherHull.vertices)
|
||||
{
|
||||
if (IsPointIn(v))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private List<Simplex> buildSimplexList()
|
||||
{
|
||||
List<Simplex> result = new List<Simplex>();
|
||||
|
||||
// Not asserted but assumed: at least three vertices
|
||||
for (int i = 0; i < vertices.Count - 1; i++)
|
||||
{
|
||||
Simplex s = new Simplex(vertices[i], vertices[i + 1]);
|
||||
result.Add(s);
|
||||
}
|
||||
Simplex s1 = new Simplex(vertices[vertices.Count - 1], vertices[0]);
|
||||
result.Add(s1);
|
||||
|
||||
if (holeVertices.Count == 0)
|
||||
return result;
|
||||
|
||||
// Same here. At least three vertices in hole assumed
|
||||
for (int i = 0; i < holeVertices.Count - 1; i++)
|
||||
{
|
||||
Simplex s = new Simplex(holeVertices[i], holeVertices[i + 1]);
|
||||
result.Add(s);
|
||||
}
|
||||
|
||||
s1 = new Simplex(holeVertices[holeVertices.Count - 1], holeVertices[0]);
|
||||
result.Add(s1);
|
||||
return result;
|
||||
}
|
||||
|
||||
// TODO: unused
|
||||
// private bool InsertVertex(Vertex v, int iAfter)
|
||||
// {
|
||||
// vertices.Insert(iAfter + 1, v);
|
||||
// return true;
|
||||
// }
|
||||
|
||||
private Vertex getNextVertex(Vertex currentVertex)
|
||||
{
|
||||
int iCurrentIndex;
|
||||
iCurrentIndex = vertices.IndexOf(currentVertex);
|
||||
|
||||
// Error handling for iCurrentIndex==-1 should go here (and probably never will)
|
||||
|
||||
iCurrentIndex++;
|
||||
if (iCurrentIndex == vertices.Count)
|
||||
iCurrentIndex = 0;
|
||||
|
||||
return vertices[iCurrentIndex];
|
||||
}
|
||||
|
||||
public Vertex FindVertex(Vertex vBase, float tolerance)
|
||||
{
|
||||
foreach (Vertex v in vertices)
|
||||
{
|
||||
if (v.IsIdentical(vBase, tolerance))
|
||||
return v;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void FindIntersection(Simplex s, ref Vertex Intersection, ref Vertex nextVertex)
|
||||
{
|
||||
Vertex bestIntersection = null;
|
||||
float distToV1 = Single.PositiveInfinity;
|
||||
Simplex bestIntersectingSimplex = null;
|
||||
|
||||
List<Simplex> simple = buildSimplexList();
|
||||
foreach (Simplex sTest in simple)
|
||||
{
|
||||
PhysicsVector vvTemp = Simplex.Intersect(sTest, s, -.001f, -.001f, 0.999f, .999f);
|
||||
|
||||
Vertex vTemp = null;
|
||||
if (vvTemp != null)
|
||||
vTemp = new Vertex(vvTemp);
|
||||
|
||||
if (vTemp != null)
|
||||
{
|
||||
PhysicsVector diff = (s.v1 - vTemp);
|
||||
float distTemp = diff.length();
|
||||
|
||||
if (bestIntersection == null || distTemp < distToV1)
|
||||
{
|
||||
bestIntersection = vTemp;
|
||||
distToV1 = distTemp;
|
||||
bestIntersectingSimplex = sTest;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Intersection = bestIntersection;
|
||||
if (bestIntersectingSimplex != null)
|
||||
nextVertex = bestIntersectingSimplex.v2;
|
||||
else
|
||||
nextVertex = null;
|
||||
}
|
||||
|
||||
|
||||
public static SimpleHull SubtractHull(SimpleHull baseHull, SimpleHull otherHull)
|
||||
{
|
||||
SimpleHull baseHullClone = baseHull.Clone();
|
||||
SimpleHull otherHullClone = otherHull.Clone();
|
||||
bool intersects = false;
|
||||
|
||||
//m_log.Debug("State before intersection detection");
|
||||
//m_log.DebugFormat("The baseHull is:\n{1}", 0, baseHullClone.ToString());
|
||||
//m_log.DebugFormat("The otherHull is:\n{1}", 0, otherHullClone.ToString());
|
||||
|
||||
{
|
||||
int iBase, iOther;
|
||||
|
||||
// Insert into baseHull
|
||||
for (iBase = 0; iBase < baseHullClone.vertices.Count; iBase++)
|
||||
{
|
||||
int iBaseNext = (iBase + 1)%baseHullClone.vertices.Count;
|
||||
Simplex sBase = new Simplex(baseHullClone.vertices[iBase], baseHullClone.vertices[iBaseNext]);
|
||||
|
||||
for (iOther = 0; iOther < otherHullClone.vertices.Count; iOther++)
|
||||
{
|
||||
int iOtherNext = (iOther + 1)%otherHullClone.vertices.Count;
|
||||
Simplex sOther =
|
||||
new Simplex(otherHullClone.vertices[iOther], otherHullClone.vertices[iOtherNext]);
|
||||
|
||||
PhysicsVector intersect = Simplex.Intersect(sBase, sOther, 0.001f, -.001f, 0.999f, 1.001f);
|
||||
if (intersect != null)
|
||||
{
|
||||
Vertex vIntersect = new Vertex(intersect);
|
||||
baseHullClone.vertices.Insert(iBase + 1, vIntersect);
|
||||
sBase.v2 = vIntersect;
|
||||
intersects = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//m_log.Debug("State after intersection detection for the base hull");
|
||||
//m_log.DebugFormat("The baseHull is:\n{1}", 0, baseHullClone.ToString());
|
||||
|
||||
{
|
||||
int iOther, iBase;
|
||||
|
||||
// Insert into otherHull
|
||||
for (iOther = 0; iOther < otherHullClone.vertices.Count; iOther++)
|
||||
{
|
||||
int iOtherNext = (iOther + 1)%otherHullClone.vertices.Count;
|
||||
Simplex sOther = new Simplex(otherHullClone.vertices[iOther], otherHullClone.vertices[iOtherNext]);
|
||||
|
||||
for (iBase = 0; iBase < baseHullClone.vertices.Count; iBase++)
|
||||
{
|
||||
int iBaseNext = (iBase + 1)%baseHullClone.vertices.Count;
|
||||
Simplex sBase = new Simplex(baseHullClone.vertices[iBase], baseHullClone.vertices[iBaseNext]);
|
||||
|
||||
PhysicsVector intersect = Simplex.Intersect(sBase, sOther, -.001f, 0.001f, 1.001f, 0.999f);
|
||||
if (intersect != null)
|
||||
{
|
||||
Vertex vIntersect = new Vertex(intersect);
|
||||
otherHullClone.vertices.Insert(iOther + 1, vIntersect);
|
||||
sOther.v2 = vIntersect;
|
||||
intersects = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//m_log.Debug("State after intersection detection for the base hull");
|
||||
//m_log.DebugFormat("The otherHull is:\n{1}", 0, otherHullClone.ToString());
|
||||
|
||||
bool otherIsInBase = baseHullClone.containsPointsFrom(otherHullClone);
|
||||
if (!intersects && otherIsInBase)
|
||||
{
|
||||
// We have a hole here
|
||||
baseHullClone.holeVertices = otherHullClone.vertices;
|
||||
return baseHullClone;
|
||||
}
|
||||
|
||||
SimpleHull result = new SimpleHull();
|
||||
|
||||
// Find a good starting Simplex from baseHull
|
||||
// A good starting simplex is one that is outside otherHull
|
||||
// Such a simplex must exist, otherwise the result will be empty
|
||||
Vertex baseStartVertex = null;
|
||||
{
|
||||
int iBase;
|
||||
for (iBase = 0; iBase < baseHullClone.vertices.Count; iBase++)
|
||||
{
|
||||
int iBaseNext = (iBase + 1)%baseHullClone.vertices.Count;
|
||||
Vertex center = new Vertex((baseHullClone.vertices[iBase] + baseHullClone.vertices[iBaseNext])/2.0f);
|
||||
bool isOutside = !otherHullClone.IsPointIn(center);
|
||||
if (isOutside)
|
||||
{
|
||||
baseStartVertex = baseHullClone.vertices[iBaseNext];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (baseStartVertex == null) // i.e. no simplex fulfilled the "outside" condition.
|
||||
// In otherwords, subtractHull completely embraces baseHull
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
// The simplex that *starts* with baseStartVertex is outside the cutting hull,
|
||||
// so we can start our walk with the next vertex without loosing a branch
|
||||
Vertex V1 = baseStartVertex;
|
||||
bool onBase = true;
|
||||
|
||||
// And here is how we do the magic :-)
|
||||
// Start on the base hull.
|
||||
// Walk the vertices in the positive direction
|
||||
// For each vertex check, whether it is a vertex shared with the other hull
|
||||
// if this is the case, switch over to walking the other vertex list.
|
||||
// Note: The other hull *must* go backwards to our starting point (via several orther vertices)
|
||||
// Thus it is important that the cutting hull has the inverse directional sense than the
|
||||
// base hull!!!!!!!!! (means if base goes CW around it's center cutting hull must go CCW)
|
||||
|
||||
bool done = false;
|
||||
while (!done)
|
||||
{
|
||||
result.AddVertex(V1);
|
||||
Vertex nextVertex = null;
|
||||
if (onBase)
|
||||
{
|
||||
nextVertex = otherHullClone.FindVertex(V1, 0.001f);
|
||||
}
|
||||
else
|
||||
{
|
||||
nextVertex = baseHullClone.FindVertex(V1, 0.001f);
|
||||
}
|
||||
|
||||
if (nextVertex != null) // A node that represents an intersection
|
||||
{
|
||||
V1 = nextVertex; // Needed to find the next vertex on the other hull
|
||||
onBase = !onBase;
|
||||
}
|
||||
|
||||
if (onBase)
|
||||
V1 = baseHullClone.getNextVertex(V1);
|
||||
else
|
||||
V1 = otherHullClone.getNextVertex(V1);
|
||||
|
||||
if (V1 == baseStartVertex)
|
||||
done = true;
|
||||
}
|
||||
|
||||
//m_log.DebugFormat("The resulting Hull is:\n{1}", 0, result.ToString());
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,220 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) Contributors, http://opensimulator.org/
|
||||
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of the OpenSim Project nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using OpenSim.Region.Physics.Manager;
|
||||
|
||||
namespace OpenSim.Region.Physics.Meshing
|
||||
{
|
||||
// A simplex is a section of a straight line.
|
||||
// It is defined by its endpoints, i.e. by two vertices
|
||||
// Operation on vertices are
|
||||
public class Simplex : IComparable<Simplex>
|
||||
{
|
||||
public Vertex v1;
|
||||
public Vertex v2;
|
||||
|
||||
public Simplex(Vertex _v1, Vertex _v2)
|
||||
{
|
||||
v1 = _v1;
|
||||
v2 = _v2;
|
||||
}
|
||||
|
||||
public int CompareTo(Simplex other)
|
||||
{
|
||||
Vertex lv1, lv2, ov1, ov2, temp;
|
||||
|
||||
lv1 = v1;
|
||||
lv2 = v2;
|
||||
ov1 = other.v1;
|
||||
ov2 = other.v2;
|
||||
|
||||
if (lv1 > lv2)
|
||||
{
|
||||
temp = lv1;
|
||||
lv1 = lv2;
|
||||
lv2 = temp;
|
||||
}
|
||||
|
||||
if (ov1 > ov2)
|
||||
{
|
||||
temp = ov1;
|
||||
ov1 = ov2;
|
||||
ov2 = temp;
|
||||
}
|
||||
|
||||
if (lv1 > ov1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (lv1 < ov1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (lv2 > ov2)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (lv2 < ov2)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static void intersectParameter(PhysicsVector p1, PhysicsVector r1, PhysicsVector p2, PhysicsVector r2,
|
||||
ref float lambda, ref float mu)
|
||||
{
|
||||
// Intersects two straights
|
||||
// p1, p2, points on the straight
|
||||
// r1, r2, directional vectors of the straight. Not necessarily of length 1!
|
||||
// note, that l, m can be scaled such, that the range 0..1 is mapped to the area between two points,
|
||||
// thus allowing to decide whether an intersection is between two points
|
||||
|
||||
float r1x = r1.X;
|
||||
float r1y = r1.Y;
|
||||
float r2x = r2.X;
|
||||
float r2y = r2.Y;
|
||||
|
||||
float denom = r1y*r2x - r1x*r2y;
|
||||
|
||||
float p1x = p1.X;
|
||||
float p1y = p1.Y;
|
||||
float p2x = p2.X;
|
||||
float p2y = p2.Y;
|
||||
|
||||
float z1 = -p2x*r2y + p1x*r2y + (p2y - p1y)*r2x;
|
||||
float z2 = -p2x*r1y + p1x*r1y + (p2y - p1y)*r1x;
|
||||
|
||||
if (denom == 0.0f) // Means the straights are parallel. Either no intersection or an infinite number of them
|
||||
{
|
||||
if (z1 == 0.0f)
|
||||
{
|
||||
// Means they are identical -> many, many intersections
|
||||
lambda = Single.NaN;
|
||||
mu = Single.NaN;
|
||||
}
|
||||
else
|
||||
{
|
||||
lambda = Single.PositiveInfinity;
|
||||
mu = Single.PositiveInfinity;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
lambda = z1/denom;
|
||||
mu = z2/denom;
|
||||
}
|
||||
|
||||
|
||||
// Intersects the simplex with another one.
|
||||
// the borders are used to deal with float inaccuracies
|
||||
// As a rule of thumb, the borders are
|
||||
// lowerBorder1 : 0.0
|
||||
// lowerBorder2 : 0.0
|
||||
// upperBorder1 : 1.0
|
||||
// upperBorder2 : 1.0
|
||||
// Set these to values near the given parameters (e.g. 0.001 instead of 1 to exclude simplex starts safely, or to -0.001 to include them safely)
|
||||
public static PhysicsVector Intersect(
|
||||
Simplex s1,
|
||||
Simplex s2,
|
||||
float lowerBorder1,
|
||||
float lowerBorder2,
|
||||
float upperBorder1,
|
||||
float upperBorder2)
|
||||
{
|
||||
PhysicsVector firstSimplexDirection = s1.v2 - s1.v1;
|
||||
PhysicsVector secondSimplexDirection = s2.v2 - s2.v1;
|
||||
|
||||
float lambda = 0.0f;
|
||||
float mu = 0.0f;
|
||||
|
||||
// Give us the parameters of an intersection. This subroutine does *not* take the constraints
|
||||
// (intersection must be between v1 and v2 and it must be in the positive direction of the ray)
|
||||
// into account. We do that afterwards.
|
||||
intersectParameter(s1.v1, firstSimplexDirection, s2.v1, secondSimplexDirection, ref lambda, ref mu);
|
||||
|
||||
if (Single.IsInfinity(lambda)) // Special case. No intersection at all. directions parallel.
|
||||
return null;
|
||||
|
||||
if (Single.IsNaN(lambda)) // Special case. many, many intersections.
|
||||
return null;
|
||||
|
||||
if (lambda > upperBorder1) // We're behind v2
|
||||
return null;
|
||||
|
||||
if (lambda < lowerBorder1)
|
||||
return null;
|
||||
|
||||
if (mu < lowerBorder2) // outside simplex 2
|
||||
return null;
|
||||
|
||||
if (mu > upperBorder2) // outside simplex 2
|
||||
return null;
|
||||
|
||||
return s1.v1 + lambda*firstSimplexDirection;
|
||||
}
|
||||
|
||||
// Intersects the simplex with a ray. The ray is defined as all p=origin + lambda*direction
|
||||
// where lambda >= 0
|
||||
public PhysicsVector RayIntersect(Vertex origin, PhysicsVector direction, bool bEndsIncluded)
|
||||
{
|
||||
PhysicsVector simplexDirection = v2 - v1;
|
||||
|
||||
float lambda = 0.0f;
|
||||
float mu = 0.0f;
|
||||
|
||||
// Give us the parameters of an intersection. This subroutine does *not* take the constraints
|
||||
// (intersection must be between v1 and v2 and it must be in the positive direction of the ray)
|
||||
// into account. We do that afterwards.
|
||||
intersectParameter(v1, simplexDirection, origin, direction, ref lambda, ref mu);
|
||||
|
||||
if (Single.IsInfinity(lambda)) // Special case. No intersection at all. directions parallel.
|
||||
return null;
|
||||
|
||||
if (Single.IsNaN(lambda)) // Special case. many, many intersections.
|
||||
return null;
|
||||
|
||||
if (mu < 0.0) // We're on the wrong side of the ray
|
||||
return null;
|
||||
|
||||
if (lambda > 1.0) // We're behind v2
|
||||
return null;
|
||||
|
||||
if (lambda == 1.0 && !bEndsIncluded)
|
||||
return null; // The end of the simplices are not included
|
||||
|
||||
if (lambda < 0.0f) // we're before v1;
|
||||
return null;
|
||||
|
||||
return v1 + lambda*simplexDirection;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue