123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656 |
- /*
- * Copyright (c) Contributors
- * 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 OpenSimulator 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 System.Text;
- using System.IO;
- using System.Xml.XPath;
- namespace PrimMesher
- {
- public struct Quat
- {
- /// <summary>X value</summary>
- public float X;
- /// <summary>Y value</summary>
- public float Y;
- /// <summary>Z value</summary>
- public float Z;
- /// <summary>W value</summary>
- public float W;
- public enum MainAxis : int
- {
- X = 0,
- Y = 1,
- Z = 2
- }
- public Quat(float x, float y, float z, float w)
- {
- X = x;
- Y = y;
- Z = z;
- W = w;
- }
- public Quat(MainAxis BaseAxis, float angle)
- {
- switch (BaseAxis)
- {
- case MainAxis.X:
- W = MathF.Cos(0.5f * angle);
- X = MathF.Sqrt(1.0f - W * W);
- Y = 0;
- Z = 0;
- break;
- case MainAxis.Y:
- W = MathF.Cos(0.5f * angle);
- Y = MathF.Sqrt(1.0f - W * W);
- X = 0;
- Z = 0;
- break;
- case MainAxis.Z:
- W = MathF.Cos(0.5f * angle);
- Z = MathF.Sqrt(1.0f - W * W);
- X = 0;
- Y = 0;
- break;
- default: //error
- X = 0;
- Y = 0;
- Z = 0;
- W = 1;
- break;
- }
- }
- public float Length()
- {
- return MathF.Sqrt(X * X + Y * Y + Z * Z + W * W);
- }
- public Quat Normalize()
- {
- const float MAG_THRESHOLD = 0.0000001f;
- float mag = (X * X + Y * Y + Z * Z + W * W);
- // Catch very small rounding errors when normalizing
- if (mag > MAG_THRESHOLD)
- {
- float oomag = 1f / MathF.Sqrt(mag);
- X *= oomag;
- Y *= oomag;
- Z *= oomag;
- W *= oomag;
- }
- else
- {
- X = 0f;
- Y = 0f;
- Z = 0f;
- W = 1f;
- }
- return this;
- }
- public static Quat operator *(Quat q1, Quat q2)
- {
- float x = q1.W * q2.X + q1.X * q2.W + q1.Y * q2.Z - q1.Z * q2.Y;
- float y = q1.W * q2.Y - q1.X * q2.Z + q1.Y * q2.W + q1.Z * q2.X;
- float z = q1.W * q2.Z + q1.X * q2.Y - q1.Y * q2.X + q1.Z * q2.W;
- float w = q1.W * q2.W - q1.X * q2.X - q1.Y * q2.Y - q1.Z * q2.Z;
- return new Quat(x, y, z, w);
- }
- public override string ToString()
- {
- return "< X: " + X.ToString() + ", Y: " + Y.ToString() + ", Z: " + Z.ToString() + ", W: " + W.ToString() + ">";
- }
- }
- public struct Coord
- {
- public float X;
- public float Y;
- public float Z;
- public Coord(float x, float y, float z)
- {
- X = x;
- Y = y;
- Z = z;
- }
- public float Length()
- {
- return MathF.Sqrt(X * X + Y * Y + Z * Z);
- }
- public Coord Invert()
- {
- X = -X;
- Y = -Y;
- Z = -Z;
- return this;
- }
- public Coord Normalize()
- {
- const float MAG_THRESHOLD = 0.0000001f;
- float mag = Length();
- // Catch very small rounding errors when normalizing
- if (mag > MAG_THRESHOLD)
- {
- float oomag = 1.0f / mag;
- X *= oomag;
- Y *= oomag;
- Z *= oomag;
- }
- else
- {
- X = 0.0f;
- Y = 0.0f;
- Z = 0.0f;
- }
- return this;
- }
- public override string ToString()
- {
- return "<"+X.ToString() + "," + Y.ToString() + "," + Z.ToString()+ ">";
- }
- public static Coord Cross(Coord c1, Coord c2)
- {
- return new Coord(
- c1.Y * c2.Z - c2.Y * c1.Z,
- c1.Z * c2.X - c2.Z * c1.X,
- c1.X * c2.Y - c2.X * c1.Y
- );
- }
- public static Coord operator +(Coord v, Coord a)
- {
- return new Coord(v.X + a.X, v.Y + a.Y, v.Z + a.Z);
- }
- public static Coord operator *(Coord v, Coord m)
- {
- return new Coord(v.X * m.X, v.Y * m.Y, v.Z * m.Z);
- }
- public static Coord operator *(Coord v, Quat q)
- {
- float x = q.X;
- float y = q.Y;
- float x2 = x + x;
- float z = q.Z;
- float y2 = y + y;
- float w = q.W;
- float z2 = z + z;
- float zz2 = z * z2;
- float xx2 = x * x2;
- float xy2 = x * y2;
- float xz2 = x * z2;
- z = v.Z;
- float yy2 = y * y2;
- float yz2 = y * z2;
- x = v.X;
- float wx2 = w * x2;
- float wy2 = w * y2;
- float wz2 = w * z2;
- y = v.Y;
- return new Coord(
- x * (1.0f - yy2 - zz2) + y * (xy2 - wz2) + z * (xz2 + wy2),
- x * (xy2 + wz2) + y * (1.0f - xx2 - zz2) + z * (yz2 - wx2),
- x * (xz2 - wy2) + y * (yz2 + wx2) + z * (1.0f - xx2 - yy2));
- }
- }
- public struct Face
- {
- public int primFace;
- // vertices
- public int v1;
- public int v2;
- public int v3;
- public Face(int _v1, int _v2, int _v3)
- {
- primFace = 0;
- v1 = _v1;
- v2 = _v2;
- v3 = _v3;
- }
- }
- internal struct Angle
- {
- internal float angle;
- internal float X;
- internal float Y;
- internal Angle(float _angle, float x, float y)
- {
- angle = _angle; // 1 is 2pi
- X = x; // cos
- Y = y; // sin
- }
- }
- internal class AngleList
- {
- private float iX, iY; // intersection point
- private static readonly Angle[] angles3 =
- {
- new Angle(0.0f, 1.0f, 0.0f),
- new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
- new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
- new Angle(1.0f, 1.0f, 0.0f)
- };
- private static readonly Angle[] angles4 =
- {
- new Angle(0.0f, 1.0f, 0.0f),
- new Angle(0.25f, 0.0f, 1.0f),
- new Angle(0.5f, -1.0f, 0.0f),
- new Angle(0.75f, 0.0f, -1.0f),
- new Angle(1.0f, 1.0f, 0.0f)
- };
- private static readonly Angle[] angles6 =
- {
- new Angle(0.0f, 1.0f, 0.0f),
- new Angle(0.16666666666666667f, 0.5f, 0.8660254037844386f),
- new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
- new Angle(0.5f, -1.0f, 0.0f),
- new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
- new Angle(0.83333333333333326f, 0.5f, -0.86602540378443904f),
- new Angle(1.0f, 1.0f, 0.0f)
- };
- private static readonly Angle[] angles12 =
- {
- new Angle(0.0f, 1.0f, 0.0f),
- new Angle(0.083333333333333329f, 0.86602540378443871f, 0.5f),
- new Angle(0.16666666666666667f, 0.5f, 0.8660254037844386f),
- new Angle(0.25f, 0.0f, 1.0f),
- new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
- new Angle(0.41666666666666663f, -0.86602540378443849f, 0.5f),
- new Angle(0.5f, -1.0f, 0.0f),
- new Angle(0.58333333333333326f, -0.86602540378443882f, -0.5f),
- new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
- new Angle(0.75f, 0.0f, -1.0f),
- new Angle(0.83333333333333326f, 0.5f, -0.86602540378443904f),
- new Angle(0.91666666666666663f, 0.86602540378443837f, -0.5f),
- new Angle(1.0f, 1.0f, 0.0f)
- };
- private static readonly Angle[] angles24 =
- {
- new Angle(0.0f, 1.0f, 0.0f),
- new Angle(0.041666666666666664f, 0.96592582628906831f, 0.25881904510252074f),
- new Angle(0.083333333333333329f, 0.86602540378443871f, 0.5f),
- new Angle(0.125f, 0.70710678118654757f, 0.70710678118654746f),
- new Angle(0.16666666666666667f, 0.5f, 0.8660254037844386f),
- new Angle(0.20833333333333331f, 0.25881904510252096f, 0.9659258262890682f),
- new Angle(0.25f, 0.0f, 1.0f),
- new Angle(0.29166666666666663f, -0.25881904510252063f, 0.96592582628906831f),
- new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
- new Angle(0.375f, -0.70710678118654746f, 0.70710678118654757f),
- new Angle(0.41666666666666663f, -0.86602540378443849f, 0.5f),
- new Angle(0.45833333333333331f, -0.9659258262890682f, 0.25881904510252102f),
- new Angle(0.5f, -1.0f, 0.0f),
- new Angle(0.54166666666666663f, -0.96592582628906842f, -0.25881904510252035f),
- new Angle(0.58333333333333326f, -0.86602540378443882f, -0.5f),
- new Angle(0.62499999999999989f, -0.70710678118654791f, -0.70710678118654713f),
- new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
- new Angle(0.70833333333333326f, -0.25881904510252152f, -0.96592582628906809f),
- new Angle(0.75f, 0.0f, -1.0f),
- new Angle(0.79166666666666663f, 0.2588190451025203f, -0.96592582628906842f),
- new Angle(0.83333333333333326f, 0.5f, -0.86602540378443904f),
- new Angle(0.875f, 0.70710678118654735f, -0.70710678118654768f),
- new Angle(0.91666666666666663f, 0.86602540378443837f, -0.5f),
- new Angle(0.95833333333333326f, 0.96592582628906809f, -0.25881904510252157f),
- new Angle(1.0f, 1.0f, 0.0f)
- };
- private static Angle interpolatePoints(float newPoint, Angle p1, Angle p2)
- {
- float m = (newPoint - p1.angle) / (p2.angle - p1.angle);
- return new Angle(newPoint, p1.X + m * (p2.X - p1.X), p1.Y + m * (p2.Y - p1.Y));
- }
- private void intersection(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
- { // ref: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
- double denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
- double uaNumerator = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
- if (denom != 0.0)
- {
- double ua = uaNumerator / denom;
- iX = (float)(x1 + ua * (x2 - x1));
- iY = (float)(y1 + ua * (y2 - y1));
- }
- }
- internal List<Angle> angles;
- internal void makeAngles(int sides, float startAngle, float stopAngle, bool hasCut)
- {
- angles = new List<Angle>();
- const float twoPi = 2.0f * MathF.PI;
- const float twoPiInv = 0.5f / MathF.PI;
- if (sides < 1)
- throw new Exception("number of sides not greater than zero");
- if (stopAngle <= startAngle)
- throw new Exception("stopAngle not greater than startAngle");
- if ((sides == 3 || sides == 4 || sides == 6 || sides == 12 || sides == 24))
- {
- startAngle *= twoPiInv;
- stopAngle *= twoPiInv;
- Angle[] sourceAngles = sides switch
- {
- 3 => angles3,
- 4 => angles4,
- 6 => angles6,
- 12 => angles12,
- _ => angles24,
- };
- int startAngleIndex = (int)(startAngle * sides);
- int endAngleIndex = sourceAngles.Length - 1;
- if (hasCut)
- {
- if (stopAngle < 1.0f)
- endAngleIndex = (int)(stopAngle * sides) + 1;
- if (endAngleIndex == startAngleIndex)
- endAngleIndex++;
- for (int angleIndex = startAngleIndex; angleIndex < endAngleIndex + 1; angleIndex++)
- {
- angles.Add(sourceAngles[angleIndex]);
- }
- if (startAngle > 0.0f)
- angles[0] = interpolatePoints(startAngle, angles[0], angles[1]);
- if (stopAngle < 1.0f)
- {
- int lastAngleIndex = angles.Count - 1;
- angles[lastAngleIndex] = interpolatePoints(stopAngle, angles[lastAngleIndex - 1], angles[lastAngleIndex]);
- }
- }
- else
- {
- for (int angleIndex = startAngleIndex; angleIndex < endAngleIndex; angleIndex++)
- angles.Add(sourceAngles[angleIndex]);
- }
- }
- else
- {
- double stepSize = twoPi / sides;
- int startStep = (int)(startAngle / stepSize);
- float angle = (float)stepSize * startStep;
- int step = startStep;
- double stopAngleTest = stopAngle;
- if (stopAngle < twoPi)
- {
- stopAngleTest = stepSize * ((int)(stopAngle / stepSize) + 1);
- if (stopAngleTest < stopAngle)
- stopAngleTest += stepSize;
- if (stopAngleTest > twoPi)
- stopAngleTest = twoPi;
- }
- while (angle <= stopAngleTest)
- {
- Angle newAngle;
- newAngle.angle = angle;
- newAngle.X = MathF.Cos(angle);
- newAngle.Y = MathF.Sin(angle);
- angles.Add(newAngle);
- step += 1;
- angle = (float)(stepSize * step);
- }
- if (startAngle > angles[0].angle)
- {
- Angle newAngle;
- intersection(angles[0].X, angles[0].Y, angles[1].X, angles[1].Y, 0.0f, 0.0f, MathF.Cos(startAngle), MathF.Sin(startAngle));
- newAngle.angle = startAngle;
- newAngle.X = iX;
- newAngle.Y = iY;
- angles[0] = newAngle;
- }
- int index = angles.Count - 1;
- if (stopAngle < angles[index].angle)
- {
- Angle newAngle;
- intersection(angles[index - 1].X, angles[index - 1].Y, angles[index].X, angles[index].Y, 0.0f, 0.0f, MathF.Cos(stopAngle), MathF.Sin(stopAngle));
- newAngle.angle = stopAngle;
- newAngle.X = iX;
- newAngle.Y = iY;
- angles[index] = newAngle;
- }
- }
- }
- }
- /// <summary>
- /// generates a profile for extrusion
- /// </summary>
- public class Profile
- {
- private const float twoPi = 2.0f * MathF.PI;
- public string errorMessage = null;
- public List<Coord> coords;
- public List<Face> faces;
- // use these for making individual meshes for each prim face
- public List<int> outerCoordIndices = null;
- public List<int> hollowCoordIndices = null;
- public int numOuterVerts = 0;
- public int numHollowVerts = 0;
- public int outerFaceNumber = -1;
- public int hollowFaceNumber = -1;
- public int bottomFaceNumber = 0;
- public int numPrimFaces = 0;
- public Profile()
- {
- coords = new List<Coord>();
- faces = new List<Face>();
- }
- public Profile(int sides, float profileStart, float profileEnd, float hollow, int hollowSides, bool hasProfileCut, bool createFaces)
- {
- const float halfSqr2 = 0.7071067811866f;
- coords = new List<Coord>();
- faces = new List<Face>();
- List<Coord> hollowCoords = new();
- bool hasHollow = (hollow > 0.0f);
- AngleList angles = new();
- AngleList hollowAngles = new();
- float xScale = 0.5f;
- float yScale = 0.5f;
- if (sides == 4) // corners of a square are sqrt(2) from center
- {
- xScale = halfSqr2;
- yScale = halfSqr2;
- }
- float startAngle = profileStart * twoPi;
- float stopAngle = profileEnd * twoPi;
- try { angles.makeAngles(sides, startAngle, stopAngle,hasProfileCut); }
- catch (Exception ex)
- {
- errorMessage = "makeAngles failed: Exception: " + ex.ToString()
- + "\nsides: " + sides.ToString() + " startAngle: " + startAngle.ToString() + " stopAngle: " + stopAngle.ToString();
- return;
- }
- numOuterVerts = angles.angles.Count;
- Angle angle;
- Coord newVert = new();
- // flag to create as few triangles as possible for 3 or 4 side profile
- bool simpleFace = (sides < 5 && !hasHollow && !hasProfileCut);
- if (hasHollow)
- {
- if (sides == hollowSides)
- hollowAngles = angles;
- else
- {
- try { hollowAngles.makeAngles(hollowSides, startAngle, stopAngle, hasProfileCut); }
- catch (Exception ex)
- {
- errorMessage = "makeAngles failed: Exception: " + ex.ToString()
- + "\nsides: " + sides.ToString() + " startAngle: " + startAngle.ToString() + " stopAngle: " + stopAngle.ToString();
- return;
- }
- int numHollowAngles = hollowAngles.angles.Count;
- for (int i = 0; i < numHollowAngles; i++)
- {
- angle = hollowAngles.angles[i];
- newVert.X = hollow * xScale * angle.X;
- newVert.Y = hollow * yScale * angle.Y;
- newVert.Z = 0.0f;
- hollowCoords.Add(newVert);
- }
- }
- numHollowVerts = hollowAngles.angles.Count;
- }
- else if (!simpleFace)
- {
- Coord center = new(0.0f, 0.0f, 0.0f);
- this.coords.Add(center);
- }
- int numAngles = angles.angles.Count;
- bool hollowsame = (hasHollow && hollowSides == sides);
- for (int i = 0; i < numAngles; i++)
- {
- angle = angles.angles[i];
- newVert.X = angle.X * xScale;
- newVert.Y = angle.Y * yScale;
- newVert.Z = 0.0f;
- coords.Add(newVert);
- if (hollowsame)
- {
- newVert.X *= hollow;
- newVert.Y *= hollow;
- hollowCoords.Add(newVert);
- }
- }
- if (hasHollow)
- {
- hollowCoords.Reverse();
- coords.AddRange(hollowCoords);
- if (createFaces)
- {
- int numTotalVerts = numOuterVerts + numHollowVerts;
- if (numOuterVerts == numHollowVerts)
- {
- Face newFace = new();
- for (int coordIndex = 0; coordIndex < numOuterVerts - 1; coordIndex++)
- {
- newFace.v1 = coordIndex;
- newFace.v2 = coordIndex + 1;
- newFace.v3 = numTotalVerts - coordIndex - 1;
- faces.Add(newFace);
- newFace.v1 = coordIndex + 1;
- newFace.v2 = numTotalVerts - coordIndex - 2;
- newFace.v3 = numTotalVerts - coordIndex - 1;
- faces.Add(newFace);
- }
- if (!hasProfileCut)
- {
- newFace.v1 = numOuterVerts - 1;
- newFace.v2 = 0;
- newFace.v3 = numOuterVerts;
- faces.Add(newFace);
- newFace.v1 = 0;
- newFace.v2 = numTotalVerts - 1;
- newFace.v3 = numOuterVerts;
- faces.Add(newFace);
- }
- }
- else if (numOuterVerts < numHollowVerts)
- {
- Face newFace = new();
- int j = 0; // j is the index for outer vertices
- int i;
- int maxJ = numOuterVerts - 1;
- float curHollowAngle = 0;
- for (i = 0; i < numHollowVerts; i++) // i is the index for inner vertices
- {
- curHollowAngle = hollowAngles.angles[i].angle;
- if (j < maxJ)
- {
- if (angles.angles[j + 1].angle - curHollowAngle < curHollowAngle - angles.angles[j].angle + 0.000001f)
- {
- newFace.v1 = numTotalVerts - i - 1;
- newFace.v2 = j;
- newFace.v3 = j + 1;
- faces.Add(newFace);
- j++;
- }
- }
- else
- {
- if (1.0f - curHollowAngle < curHollowAngle - angles.angles[j].angle + 0.000001f)
- break;
- }
- newFace.v1 = j;
- newFace.v2 = numTotalVerts - i - 2;
- newFace.v3 = numTotalVerts - i - 1;
- faces.Add(newFace);
- }
- if (!hasProfileCut)
- {
- if (i == numHollowVerts)
- {
- newFace.v1 = numTotalVerts - numHollowVerts;
- newFace.v2 = maxJ;
- newFace.v3 = 0;
- faces.Add(newFace);
- }
- else
- {
- if (1.0f - curHollowAngle < curHollowAngle - angles.angles[maxJ].angle + 0.000001f)
- {
- newFace.v1 = numTotalVerts - i - 1;
- newFace.v2 = maxJ;
- newFace.v3 = 0;
- faces.Add(newFace);
- }
- for (; i < numHollowVerts - 1; i++)
- {
- newFace.v1 = 0;
- newFace.v2 = numTotalVerts - i - 2;
- newFace.v3 = numTotalVerts - i - 1;
- faces.Add(newFace);
- }
- }
- newFace.v1 = 0;
- newFace.v2 = numTotalVerts - 1;
- newFace.v3 = numTotalVerts - numHollowVerts;
- faces.Add(newFace);
- }
- }
- else // numHollowVerts < numOuterVerts
- {
- Face newFace = new();
- int j = 0; // j is the index for inner vertices
- int maxJ = numHollowVerts - 1;
- for (int i = 0; i < numOuterVerts; i++)
- {
- if (j < maxJ)
- if (hollowAngles.angles[j + 1].angle - angles.angles[i].angle < angles.angles[i].angle - hollowAngles.angles[j].angle + 0.000001f)
- {
- newFace.v1 = i;
- newFace.v2 = numTotalVerts - j - 2;
- newFace.v3 = numTotalVerts - j - 1;
- faces.Add(newFace);
- j += 1;
- }
- newFace.v1 = numTotalVerts - j - 1;
- newFace.v2 = i;
- newFace.v3 = i + 1;
- faces.Add(newFace);
- }
- if (!hasProfileCut)
- {
- int i = numOuterVerts - 1;
- if (hollowAngles.angles[0].angle - angles.angles[i].angle < angles.angles[i].angle - hollowAngles.angles[maxJ].angle + 0.000001f)
- {
- newFace.v1 = 0;
- newFace.v2 = numTotalVerts - 1;
- newFace.v3 = numTotalVerts - maxJ - 1;
- faces.Add(newFace);
- }
- newFace.v1 = numTotalVerts - maxJ - 1;
- newFace.v2 = i;
- newFace.v3 = 0;
- faces.Add(newFace);
- }
- }
- }
- }
- else if (createFaces)
- {
- if (simpleFace)
- {
- if (sides == 3)
- faces.Add(new Face(0, 1, 2));
- else if (sides == 4)
- {
- faces.Add(new Face(0, 1, 2));
- faces.Add(new Face(0, 2, 3));
- }
- }
- else
- {
- for (int i = 1; i < numAngles ; i++)
- {
- Face newFace = new()
- {
- v1 = 0,
- v2 = i,
- v3 = i + 1
- };
- faces.Add(newFace);
- }
- if (!hasProfileCut)
- {
- Face newFace = new()
- {
- v1 = 0,
- v2 = numAngles,
- v3 = 1
- };
- faces.Add(newFace);
- }
- }
- }
- }
- public Profile Copy()
- {
- return Copy(true);
- }
- public Profile Copy(bool needFaces)
- {
- Profile copy = new();
- copy.coords.AddRange(coords);
- if (needFaces)
- copy.faces.AddRange(faces);
- copy.numOuterVerts = numOuterVerts;
- copy.numHollowVerts = numHollowVerts;
- return copy;
- }
- public void AddPos(Coord v)
- {
- this.AddPos(v.X, v.Y, v.Z);
- }
- public void AddPos(float x, float y, float z)
- {
- int i;
- int numVerts = coords.Count;
- Coord vert;
- for (i = 0; i < numVerts; i++)
- {
- vert = coords[i];
- vert.X += x;
- vert.Y += y;
- vert.Z += z;
- coords[i] = vert;
- }
- }
- public void AddRot(Quat q)
- {
- int i;
- int numVerts = coords.Count;
- for (i = 0; i < numVerts; i++)
- coords[i] *= q;
- }
- public void Scale(float x, float y)
- {
- int i;
- int numVerts = coords.Count;
- Coord vert;
- for (i = 0; i < numVerts; i++)
- {
- vert = coords[i];
- vert.X *= x;
- vert.X = MathF.Round(vert.X,5);
- vert.Y *= y;
- vert.Y = MathF.Round(vert.Y,5);
- coords[i] = vert;
- }
- if(x == 0f || y == 0f)
- faces = new List<Face>();
- }
- /// <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()
- {
- int numFaces = faces.Count;
- if(numFaces == 0)
- return;
- int i;
- Face tmpFace;
- int tmp;
- for (i = 0; i < numFaces; i++)
- {
- tmpFace = faces[i];
- tmp = tmpFace.v3;
- tmpFace.v3 = tmpFace.v1;
- tmpFace.v1 = tmp;
- faces[i] = tmpFace;
- }
- }
- public void AddValue2FaceVertexIndices(int num)
- {
- int numFaces = faces.Count;
- if(numFaces == 0)
- return;
- Face tmpFace;
- for (int i = 0; i < numFaces; i++)
- {
- tmpFace = faces[i];
- tmpFace.v1 += num;
- tmpFace.v2 += num;
- tmpFace.v3 += num;
- faces[i] = tmpFace;
- }
- }
- public void DumpRaw(String path, String name, String title)
- {
- if (path == null)
- return;
- string completePath = System.IO.Path.Combine(path, $"{name}_{title}.raw");
- using StreamWriter sw = new(completePath);
- for (int i = 0; i < faces.Count; i++)
- {
- sw.WriteLine($"{coords[faces[i].v1]} {coords[faces[i].v3]} {coords[faces[i].v3]}");
- }
- sw.Close();
- }
- }
- public struct PathNode
- {
- public Coord position;
- public Quat rotation;
- public float xScale;
- public float yScale;
- public float percentOfPath;
- }
- public enum PathType { Linear = 0, Circular = 1, Flexible = 2 }
- public class Path
- {
- public List<PathNode> pathNodes = new();
- public float twistBegin = 0.0f;
- public float twistEnd = 0.0f;
- public float topShearX = 0.0f;
- public float topShearY = 0.0f;
- public float pathCutBegin = 0.0f;
- public float pathCutEnd = 1.0f;
- public float dimpleBegin = 0.0f;
- public float dimpleEnd = 1.0f;
- public float skew = 0.0f;
- public float holeSizeX = 1.0f; // called pathScaleX in pbs
- public float holeSizeY = 0.25f;
- public float taperX = 0.0f;
- public float taperY = 0.0f;
- public float radius = 0.0f;
- public float revolutions = 1.0f;
- public int stepsPerRevolution = 24;
- private const float twoPi = 2.0f * MathF.PI;
- public void Create(PathType pathType, int steps)
- {
- if (taperX > .9999f)
- taperX = 1.0f;
- else if (taperX < -.9999f)
- taperX = -1.0f;
- if (taperY > .9999f)
- taperY = 1.0f;
- else if (taperY < -.9999f)
- taperY = -1.0f;
- if (pathType == PathType.Linear || pathType == PathType.Flexible)
- {
- int step = 0;
- float length = pathCutEnd - pathCutBegin;
- float twistTotal = twistEnd - twistBegin;
- float twistTotalAbs = MathF.Abs(twistTotal);
- if (twistTotalAbs > 0.01f)
- steps += (int)(twistTotalAbs * 3.66); // dahlia's magic number
- float start = -0.5f;
- float stepSize = length / (float)steps;
- float percentOfPathMultiplier = stepSize * 0.999999f;
- float xOffset = topShearX * pathCutBegin;
- float yOffset = topShearY * pathCutBegin;
- float zOffset = start;
- float xOffsetStepIncrement = topShearX * length / steps;
- float yOffsetStepIncrement = topShearY * length / steps;
- float percentOfPath = pathCutBegin;
- zOffset += percentOfPath;
- // sanity checks
- bool done = false;
- while (!done)
- {
- PathNode newNode = new();
- newNode.xScale = 1.0f;
- if (taperX > 0.0f)
- newNode.xScale -= percentOfPath * taperX;
- else if(taperX < 0.0f)
- newNode.xScale += (1.0f - percentOfPath) * taperX;
- newNode.yScale = 1.0f;
- if (taperY > 0.0f)
- newNode.yScale -= percentOfPath * taperY;
- else if(taperY < 0.0f)
- newNode.yScale += (1.0f - percentOfPath) * taperY;
- float twist = twistBegin + twistTotal * percentOfPath;
- newNode.rotation = new Quat(Quat.MainAxis.Z, twist);
- newNode.position = new Coord(xOffset, yOffset, zOffset);
- newNode.percentOfPath = percentOfPath;
- pathNodes.Add(newNode);
- if (step < steps)
- {
- step += 1;
- percentOfPath += percentOfPathMultiplier;
- xOffset += xOffsetStepIncrement;
- yOffset += yOffsetStepIncrement;
- zOffset += stepSize;
- if (percentOfPath > pathCutEnd)
- done = true;
- }
- else done = true;
- }
- } // end of linear path code
- else // pathType == Circular
- {
- float twistTotal = twistEnd - twistBegin;
- // 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
- float twistTotalAbs = MathF.Abs(twistTotal);
- if (twistTotalAbs > 0.01f)
- {
- if (twistTotalAbs > MathF.PI * 1.5f)
- steps *= 2;
- if (twistTotalAbs > MathF.PI * 3.0f)
- steps *= 2;
- }
- float yPathScale = holeSizeY * 0.5f;
- float pathLength = pathCutEnd - pathCutBegin;
- float totalSkew = skew * 2.0f * pathLength;
- float skewStart = pathCutBegin * 2.0f * skew - skew;
- float xOffsetTopShearXFactor = topShearX * (0.25f + 0.5f * (0.5f - holeSizeY));
- float yShearCompensation = 1.0f + MathF.Abs(topShearY) * 0.25f;
- // 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 = (twoPi * pathCutBegin * revolutions) - topShearY * 0.9f;
- float endAngle = (twoPi * pathCutEnd * revolutions) - topShearY * 0.9f;
- float stepSize = twoPi / stepsPerRevolution;
- int step = (int)(startAngle / stepSize);
- float angle = startAngle;
- bool done = false;
- while (!done) // loop through the length of the path and add the layers
- {
- PathNode newNode = new();
- float xProfileScale = (1.0f - MathF.Abs(skew)) * holeSizeX;
- float yProfileScale = holeSizeY;
- float percentOfPath = angle / (twoPi * revolutions);
- float percentOfAngles = (angle - startAngle) / (endAngle - startAngle);
- if (taperX > 0.01f)
- xProfileScale *= 1.0f - percentOfPath * taperX;
- else if (taperX < -0.01f)
- xProfileScale *= 1.0f + (1.0f - percentOfPath) * taperX;
- if (taperY > 0.01f)
- yProfileScale *= 1.0f - percentOfPath * taperY;
- else if (taperY < -0.01f)
- yProfileScale *= 1.0f + (1.0f - percentOfPath) * taperY;
- newNode.xScale = xProfileScale;
- newNode.yScale = yProfileScale;
- float radiusScale = 1.0f;
- if (radius > 0.001f)
- radiusScale = 1.0f - radius * percentOfPath;
- else if (radius < 0.001f)
- radiusScale = 1.0f + radius * (1.0f - percentOfPath);
- float twist = twistBegin + twistTotal * percentOfPath;
- float xOffset = 0.5f * (skewStart + totalSkew * percentOfAngles);
- xOffset += MathF.Sin(angle) * xOffsetTopShearXFactor;
- float yOffset = yShearCompensation * MathF.Cos(angle) * (0.5f - yPathScale) * radiusScale;
- float zOffset = MathF.Sin(angle + topShearY) * (0.5f - yPathScale) * radiusScale;
- newNode.position = new Coord(xOffset, yOffset, zOffset);
- // now orient the rotation of the profile layer relative to it's position on the path
- // adding taperY to the angle used to generate the quat appears to approximate the viewer
- newNode.rotation = new Quat(Quat.MainAxis.X, angle + topShearY);
- // next apply twist rotation to the profile layer
- if (twistTotal != 0.0f || twistBegin != 0.0f)
- newNode.rotation *= new Quat(Quat.MainAxis.Z, twist);
- newNode.percentOfPath = percentOfPath;
- pathNodes.Add(newNode);
- // calculate terms for next iteration
- // calculate the angle for the next iteration of the loop
- if (angle >= endAngle - 0.01)
- done = true;
- else
- {
- step += 1;
- angle = stepSize * step;
- if (angle > endAngle)
- angle = endAngle;
- }
- }
- }
- }
- }
- public class PrimMesh
- {
- public string errorMessage = "";
- public List<Coord> coords;
- // public List<Coord> normals;
- public List<Face> faces;
- private int sides = 4;
- private int hollowSides = 4;
- private float profileStart = 0.0f;
- private float profileEnd = 1.0f;
- private float hollow = 0.0f;
- public float twistBegin = 0;
- public float twistEnd = 0;
- public float topShearX = 0.0f;
- public float topShearY = 0.0f;
- public float pathCutBegin = 0.0f;
- public float pathCutEnd = 1.0f;
- public float dimpleBegin = 0.0f;
- public float dimpleEnd = 1.0f;
- public float skew = 0.0f;
- public float holeSizeX = 1.0f; // called pathScaleX in pbs
- public float holeSizeY = 0.25f;
- public float taperX = 0.0f;
- public float taperY = 0.0f;
- public float radius = 0.0f;
- public float revolutions = 1.0f;
- public int stepsPerRevolution = 24;
- private bool hasProfileCut = false;
- private bool hasHollow = 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 = "";
- s += "sides..................: " + this.sides.ToString();
- s += "\nhollowSides..........: " + this.hollowSides.ToString();
- s += "\nprofileStart.........: " + this.profileStart.ToString();
- s += "\nprofileEnd...........: " + this.profileEnd.ToString();
- s += "\nhollow...............: " + this.hollow.ToString();
- s += "\ntwistBegin...........: " + this.twistBegin.ToString();
- s += "\ntwistEnd.............: " + this.twistEnd.ToString();
- s += "\ntopShearX............: " + this.topShearX.ToString();
- s += "\ntopShearY............: " + this.topShearY.ToString();
- s += "\npathCutBegin.........: " + this.pathCutBegin.ToString();
- s += "\npathCutEnd...........: " + this.pathCutEnd.ToString();
- s += "\ndimpleBegin..........: " + this.dimpleBegin.ToString();
- s += "\ndimpleEnd............: " + this.dimpleEnd.ToString();
- s += "\nskew.................: " + this.skew.ToString();
- s += "\nholeSizeX............: " + this.holeSizeX.ToString();
- s += "\nholeSizeY............: " + this.holeSizeY.ToString();
- s += "\ntaperX...............: " + this.taperX.ToString();
- s += "\ntaperY...............: " + this.taperY.ToString();
- s += "\nradius...............: " + this.radius.ToString();
- s += "\nrevolutions..........: " + this.revolutions.ToString();
- s += "\nstepsPerRevolution...: " + this.stepsPerRevolution.ToString();
- s += "\nhasProfileCut........: " + this.hasProfileCut.ToString();
- s += "\nhasHollow............: " + this.hasHollow.ToString();
- return s;
- }
- public bool HasProfileCut
- {
- get { return hasProfileCut; }
- set { hasProfileCut = value; }
- }
- public bool HasHollow
- {
- get { return hasHollow; }
- }
- /// <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>
- /// <param name="sphereMode"></param>
- public PrimMesh(int _sides, float _profileStart, float _profileEnd, float _hollow, int _hollowSides)
- {
- coords = new List<Coord>();
- faces = new List<Face>();
- sides = _sides;
- profileStart = _profileStart;
- profileEnd = _profileEnd;
- hollow = _hollow;
- hollowSides = _hollowSides;
- if (sides < 3)
- sides = 3;
- if (hollowSides < 3)
- hollowSides = 3;
- if (profileStart < 0.0f)
- profileStart = 0.0f;
- if (profileEnd > 1.0f)
- profileEnd = 1.0f;
- else if (profileEnd < 0.02f)
- profileEnd = 0.02f;
- if (profileStart >= profileEnd)
- profileStart = profileEnd - 0.02f;
- if (hollow > 0.99f)
- hollow = 0.99f;
- else if (hollow < 0.0f)
- hollow = 0.0f;
- }
- /// <summary>
- /// Extrudes a profile along a path.
- /// </summary>
- public void Extrude(PathType pathType)
- {
- bool needEndFaces;
- coords = new List<Coord>();
- faces = new List<Face>();
- int steps = 1;
- //float length = pathCutEnd - pathCutBegin;
- hasProfileCut = profileEnd - profileStart < 0.9999f;
- float twistTotal = twistEnd - twistBegin;
- float twistTotalAbs = MathF.Abs(twistTotal);
- if (twistTotalAbs > 0.01f)
- steps += (int)(twistTotalAbs * 3.66); // dahlia's magic number
- float hollow = this.hollow;
- hasHollow = hollow > 0.001f;
- float initialProfileRot = 0.0f;
- if (pathType == PathType.Circular)
- {
- needEndFaces = false;
- if (pathCutBegin != 0.0f || pathCutEnd != 1.0f)
- needEndFaces = true;
- else if (taperX != 0.0f || taperY != 0.0f)
- needEndFaces = true;
- else if (skew != 0.0f)
- needEndFaces = true;
- else if (twistTotal != 0.0f)
- needEndFaces = true;
- else if (radius != 0.0f)
- needEndFaces = true;
- }
- else needEndFaces = true;
- if (pathType == PathType.Circular)
- {
- if (sides == 3)
- {
- initialProfileRot = MathF.PI;
- if (hollowSides == 4)
- {
- if (hollow > 0.7f)
- hollow = 0.7f;
- hollow *= 0.707f;
- }
- else hollow *= 0.5f;
- }
- else if (sides == 4)
- {
- initialProfileRot = 0.25f * MathF.PI;
- if (hollowSides != 4)
- hollow *= 0.707f;
- }
- else if (sides > 4)
- {
- initialProfileRot = MathF.PI;
- if (hollowSides == 4)
- {
- if (hollow > 0.7f)
- hollow = 0.7f;
- hollow /= 0.7f;
- }
- }
- }
- else
- {
- if (sides == 3)
- {
- if (hollowSides == 4)
- {
- if (hollow > 0.7f)
- hollow = 0.7f;
- hollow *= 0.707f;
- }
- else hollow *= 0.5f;
- }
- else if (sides == 4)
- {
- initialProfileRot = 1.25f * MathF.PI;
- if (hollowSides != 4)
- hollow *= 0.707f;
- }
- else if (sides == 24 && hollowSides == 4)
- hollow *= 1.414f;
- }
- Profile profile = new(sides, profileStart, profileEnd, hollow, hollowSides, HasProfileCut, true);
- errorMessage = profile.errorMessage;
- numPrimFaces = profile.numPrimFaces;
- if (initialProfileRot != 0.0f)
- {
- profile.AddRot(new Quat(Quat.MainAxis.Z, initialProfileRot));
- }
- Path path = new()
- {
- twistBegin = twistBegin,
- twistEnd = twistEnd,
- topShearX = topShearX,
- topShearY = topShearY,
- pathCutBegin = pathCutBegin,
- pathCutEnd = pathCutEnd,
- dimpleBegin = dimpleBegin,
- dimpleEnd = dimpleEnd,
- skew = skew,
- holeSizeX = holeSizeX,
- holeSizeY = holeSizeY,
- taperX = taperX,
- taperY = taperY,
- radius = radius,
- revolutions = revolutions,
- stepsPerRevolution = stepsPerRevolution
- };
- path.Create(pathType, steps);
- int lastNode = path.pathNodes.Count - 1;
- for (int nodeIndex = 0; nodeIndex < path.pathNodes.Count; nodeIndex++)
- {
- PathNode node = path.pathNodes[nodeIndex];
- Profile newLayer = profile.Copy();
- newLayer.Scale(node.xScale, node.yScale);
- newLayer.AddRot(node.rotation);
- newLayer.AddPos(node.position);
- // append this layer
- int coordsStart = coords.Count;
- coords.AddRange(newLayer.coords);
- if (needEndFaces && nodeIndex == 0 && newLayer.faces.Count > 0)
- {
- newLayer.AddValue2FaceVertexIndices(coordsStart);
- newLayer.FlipNormals();
- faces.AddRange(newLayer.faces);
- }
- // fill faces between layers
- List<Face> linkfaces = new();
- int numVerts = newLayer.coords.Count;
- Face newFace1 = new();
- Face newFace2 = new();
- if (nodeIndex > 0)
- {
- int startVert = coordsStart;
- int endVert = coords.Count;
- if (!hasProfileCut)
- {
- if(numVerts > 5 && !hasHollow)
- startVert++;
- int i = startVert;
- for (int l = 0; l < profile.numOuterVerts - 1; l++)
- {
- newFace1.v1 = i;
- newFace1.v2 = i - numVerts;
- newFace1.v3 = i + 1;
- linkfaces.Add(newFace1);
- newFace2.v1 = i + 1;
- newFace2.v2 = i - numVerts;
- newFace2.v3 = i + 1 - numVerts;
- linkfaces.Add(newFace2);
- i++;
- }
- newFace1.v1 = i;
- newFace1.v2 = i - numVerts;
- newFace1.v3 = startVert;
- linkfaces.Add(newFace1);
- newFace2.v1 = startVert;
- newFace2.v2 = i - numVerts;
- newFace2.v3 = startVert - numVerts;
- linkfaces.Add(newFace2);
- if (hasHollow)
- {
- startVert = ++i;
- for (int l = 0; l < profile.numHollowVerts - 1; l++)
- {
- newFace1.v1 = i;
- newFace1.v2 = i - numVerts;
- newFace1.v3 = i + 1;
- linkfaces.Add(newFace1);
- newFace2.v1 = i + 1;
- newFace2.v2 = i - numVerts;
- newFace2.v3 = i + 1 - numVerts;
- linkfaces.Add(newFace2);
- i++;
- }
- newFace1.v1 = i;
- newFace1.v2 = i - numVerts;
- newFace1.v3 = startVert;
- linkfaces.Add(newFace1);
- newFace2.v1 = startVert;
- newFace2.v2 = i - numVerts;
- newFace2.v3 = startVert - numVerts;
- linkfaces.Add(newFace2);
- }
- }
- else
- {
- for (int i = startVert; i < endVert; i++)
- {
- int iNext = i + 1;
- if (i == endVert - 1)
- iNext = startVert;
- newFace1.v1 = i;
- newFace1.v2 = i - numVerts;
- newFace1.v3 = iNext;
- linkfaces.Add(newFace1);
- newFace2.v1 = iNext;
- newFace2.v2 = i - numVerts;
- newFace2.v3 = iNext - numVerts;
- linkfaces.Add(newFace2);
- }
- }
- }
- if(linkfaces.Count > 0)
- faces.AddRange(linkfaces);
- if (needEndFaces && nodeIndex == lastNode && newLayer.faces.Count > 0)
- {
- newLayer.AddValue2FaceVertexIndices(coordsStart);
- faces.AddRange(newLayer.faces);
- }
- } // for (int nodeIndex = 0; nodeIndex < path.pathNodes.Count; nodeIndex++)
- // more cleanup will be done at Meshmerizer.cs
- }
- private static Coord SurfaceNormal(Coord c1, Coord c2, Coord c3)
- {
- Coord edge1 = new(c2.X - c1.X, c2.Y - c1.Y, c2.Z - c1.Z);
- Coord edge2 = new(c3.X - c1.X, c3.Y - c1.Y, c3.Z - c1.Z);
- Coord normal = Coord.Cross(edge1, edge2);
- normal.Normalize();
- return normal;
- }
- private Coord SurfaceNormal(Face face)
- {
- return SurfaceNormal(coords[face.v1], coords[face.v2], 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 = faces.Count;
- if (faceIndex < 0 || faceIndex >= numFaces)
- throw new Exception("faceIndex out of range");
- return SurfaceNormal(faces[faceIndex]);
- }
- /// <summary>
- /// Duplicates a PrimMesh object. All object properties are copied by value, including lists.
- /// </summary>
- /// <returns></returns>
- public PrimMesh Copy()
- {
- PrimMesh copy = new(sides, profileStart, profileEnd, hollow, hollowSides)
- {
- twistBegin = twistBegin,
- twistEnd = twistEnd,
- topShearX = topShearX,
- topShearY = topShearY,
- pathCutBegin = pathCutBegin,
- pathCutEnd = pathCutEnd,
- dimpleBegin = dimpleBegin,
- dimpleEnd = dimpleEnd,
- skew = skew,
- holeSizeX = holeSizeX,
- holeSizeY = holeSizeY,
- taperX = taperX,
- taperY = taperY,
- radius = radius,
- revolutions = revolutions,
- stepsPerRevolution = stepsPerRevolution,
- numPrimFaces = numPrimFaces,
- errorMessage = errorMessage,
- coords = new List<Coord>(coords),
- faces = new List<Face>(faces)
- };
- return copy;
- }
- /// <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;
- int numVerts = coords.Count;
- Coord vert;
- for (i = 0; i < numVerts; i++)
- {
- vert = coords[i];
- vert.X += x;
- vert.Y += y;
- vert.Z += z;
- coords[i] = vert;
- }
- }
- /// <summary>
- /// Rotates the mesh
- /// </summary>
- /// <param name="q"></param>
- public void AddRot(Quat q)
- {
- int i;
- int numVerts = coords.Count;
- for (i = 0; i < numVerts; i++)
- coords[i] *= q;
- }
- /// <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;
- int numVerts = this.coords.Count;
- //Coord vert;
- Coord m = new(x, y, z);
- for (i = 0; i < numVerts; i++)
- coords[i] *= m;
- }
- /// <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)
- return;
- string completePath = System.IO.Path.Combine(path, $"{name}_{title}.raw");
- using StreamWriter sw = new(completePath);
- for (int i = 0; i < this.faces.Count; i++)
- {
- sw.Write(coords[faces[i].v1].ToString());
- sw.Write(coords[faces[i].v2].ToString());
- sw.WriteLine(coords[faces[i].v3].ToString());
- }
- }
- }
- }
|