Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Algorithms/Graphics/Triangularize.cs @ 9102

Last change on this file since 9102 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 8.4 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Collections.Generic;
42using System.Text;
43
44namespace ILNumerics.Algorithms {
45    public partial class Graphic {
46
47        /// <summary>
48        /// Increase the number of triangles by doubling existing triangles
49        /// </summary>
50        /// <param name="vertices">Vertices</param>
51        /// <param name="triangles">Triangle index definitions</param>
52        /// <param name="iterations">Number of iterations, each iteration will make 4 triangles out of each triangle</param>
53        /// <param name="outVertices">[Output] Vertices.</param>
54        /// <param name="outTriangles">[Output] Triangles</param>
55        /// <remarks><para>Incoming triangles are expected not to be degenerated. This means:
56        /// Every edge is used only twice at most. No triangle shares more than 2
57        /// corners with some other triangle. </para>
58        ///   <para>None of the incoming arrays will get altered</para></remarks>
59        public static void Triangularize(ILInArray<float> vertices, ILInArray<int> triangles,
60                                        int iterations,
61                                        ILOutArray<float> outVertices,
62                                        ILOutArray<int> outTriangles) {
63            using (ILScope.Enter(vertices, triangles)) {
64                int numVertices = vertices.Size[1];
65                int numTriangles = triangles.Size[1];
66                outVertices.a = vertices.C;
67                outTriangles.a = triangles.C;
68                // being pessimistic: expect to create a larger number of vertices than probable
69                outVertices[0, numVertices * 2] = 0;
70                outTriangles[0, numTriangles * 2] = 0;
71
72                int triIndLast = numTriangles;
73                int vertIndLast = numVertices;
74                for (int it = 0; it < iterations; it++) {
75                    int triIndItEnd = triIndLast;
76
77                    for (int triInd = 0; triInd < triIndItEnd; triInd++) {
78                        int vertInd0 = outTriangles.GetValue(0, triInd);
79                        int vertInd1 = outTriangles.GetValue(1, triInd);
80                        int vertInd2 = outTriangles.GetValue(2, triInd);
81                        // create new vertices
82                        float v0x = (outVertices.GetValue(0, vertInd0) + outVertices.GetValue(0, vertInd1)) / 2f;
83                        float v0y = (outVertices.GetValue(1, vertInd0) + outVertices.GetValue(1, vertInd1)) / 2f;
84                        float v0z = (outVertices.GetValue(2, vertInd0) + outVertices.GetValue(2, vertInd1)) / 2f;
85                        float v1x = (outVertices.GetValue(0, vertInd1) + outVertices.GetValue(0, vertInd2)) / 2f;
86                        float v1y = (outVertices.GetValue(1, vertInd1) + outVertices.GetValue(1, vertInd2)) / 2f;
87                        float v1z = (outVertices.GetValue(2, vertInd1) + outVertices.GetValue(2, vertInd2)) / 2f;
88                        float v2x = (outVertices.GetValue(0, vertInd2) + outVertices.GetValue(0, vertInd0)) / 2f;
89                        float v2y = (outVertices.GetValue(1, vertInd2) + outVertices.GetValue(1, vertInd0)) / 2f;
90                        float v2z = (outVertices.GetValue(2, vertInd2) + outVertices.GetValue(2, vertInd0)) / 2f;
91                        #region new vertex exists already? TODO: This needs to be replaced with a b-tree implementation!!
92                        int newVertID0 = -1;
93                        int newVertID1 = -1;
94                        int newVertID2 = -1;
95                        for (int vi = 0; vi < vertIndLast; vi++) {
96                            float tmpX = outVertices.GetValue(0, vi);
97                            float tmpY = outVertices.GetValue(1, vi);
98                            float tmpZ = outVertices.GetValue(2, vi);
99                            if (tmpX == v0x && tmpY == v0y && tmpZ == v0z) {
100                                newVertID0 = vi;
101                            }
102                            if (tmpX == v1x && tmpY == v1y && tmpZ == v1z) {
103                                newVertID1 = vi;
104                            }
105                            if (tmpX == v2x && tmpY == v2y && tmpZ == v2z) {
106                                newVertID2 = vi;
107                            }
108                            if (newVertID0 >= 0 && newVertID1 >= 0 && newVertID2 >= 0)
109                                break;
110                        }
111                        #endregion
112
113                        if (newVertID0 < 0) {
114                            newVertID0 = vertIndLast++;
115                            outVertices[0, newVertID0] = v0x;
116                            outVertices[1, newVertID0] = v0y;
117                            outVertices[2, newVertID0] = v0z;
118                        }
119                        if (newVertID1 < 0) {
120                            newVertID1 = vertIndLast++;
121                            outVertices[0, newVertID1] = v1x;
122                            outVertices[1, newVertID1] = v1y;
123                            outVertices[2, newVertID1] = v1z;
124                        }
125                        if (newVertID2 < 0) {
126                            newVertID2 = vertIndLast++;
127                            outVertices[0, newVertID2] = v2x;
128                            outVertices[1, newVertID2] = v2y;
129                            outVertices[2, newVertID2] = v2z;
130                        }
131
132                        // create new triangles
133                        outTriangles.SetValue(newVertID0, 1, triInd);
134                        outTriangles.SetValue(newVertID2, 2, triInd);
135
136                        outTriangles.SetValue(newVertID2, 0, triIndLast);
137                        outTriangles.SetValue(newVertID1, 1, triIndLast);
138                        outTriangles.SetValue(vertInd2, 2, triIndLast++);
139
140                        outTriangles.SetValue(newVertID2, 0, triIndLast);
141                        outTriangles.SetValue(newVertID0, 1, triIndLast);
142                        outTriangles.SetValue(newVertID1, 2, triIndLast++);
143
144                        outTriangles.SetValue(newVertID0, 0, triIndLast);
145                        outTriangles.SetValue(vertInd1, 1, triIndLast);
146                        outTriangles.SetValue(newVertID1, 2, triIndLast++);
147                    }
148
149                }
150                outVertices.a = outVertices[":;0:" + (vertIndLast - 1)];
151                outTriangles.a = outTriangles[":;0:" + (triIndLast - 1)];
152            }
153        }
154    }
155}
Note: See TracBrowser for help on using the repository browser.