<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Softimage Blog &#187; Modeling</title>
	<atom:link href="http://www.softimageblog.com/archives/category/modeling/feed" rel="self" type="application/rss+xml" />
	<link>http://www.softimageblog.com</link>
	<description>People and thoughts behind Softimage in production...</description>
	<lastBuildDate>Mon, 21 Jun 2010 20:40:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Object oriented programming, once again.</title>
		<link>http://www.softimageblog.com/archives/260#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=object-oriented-programming-once-again</link>
		<comments>http://www.softimageblog.com/archives/260#comments</comments>
		<pubDate>Tue, 20 May 2008 14:32:24 +0000</pubDate>
		<dc:creator>Helge Mathee</dc:creator>
				<category><![CDATA[JScript]]></category>
		<category><![CDATA[Modeling]]></category>
		<category><![CDATA[Programming / Scripting]]></category>

		<guid isPermaLink="false">http://www.xsi-blog.com/?p=260</guid>
		<description><![CDATA[So the keen folks at the german XSI forum (http://www.xsiforum.de) discussed the topic of generating separate objects out of all of the polygon islands making up a polygonmesh. Even though this doesn&#8217;t seem to be a too tricky scripting problem, using commands to achieve this split-up can slow down XSI quite alot, especially if the [...]]]></description>
			<content:encoded><![CDATA[<p>So the keen folks at the german XSI forum (<a href="http://www.xsiforum.de">http://www.xsiforum.de</a>) discussed the topic of generating separate objects out of all of the polygon islands making up a polygonmesh. Even though this doesn&#8217;t seem to be a too tricky scripting problem, using commands to achieve this split-up can slow down XSI quite alot, especially if the source mesh contains a huge amount of polygon islands. Therefore I tried to come up with a object oriented solution, and here it is.<br />
<span id="more-260"></span></p>
<p>I put some effort into commenting the script accordingly, so that it may be used for learning purposes.</p>
<p>cheers!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// query the geometry data</span>
<span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> selection<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> geo <span style="color: #339933;">=</span> obj.<span style="color: #660066;">Activeprimitive</span>.<span style="color: #660066;">Geometry</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> polygons <span style="color: #339933;">=</span> geo.<span style="color: #660066;">Polygons</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// create an array to track which polygon has been visited</span>
<span style="color: #003366; font-weight: bold;">var</span> polygonVisited <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> polygonsLeft <span style="color: #339933;">=</span> polygons.<span style="color: #660066;">Count</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// fill up the array to say: &quot;no polygon has been visited yet&quot;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>polygons .<span style="color: #660066;">count</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	polygonVisited<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// create some helper variables</span>
<span style="color: #003366; font-weight: bold;">var</span> clusterIndices <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> currentRow <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// log the start of the process</span>
logmessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Starting with &quot;</span><span style="color: #339933;">+</span>polygonsLeft<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; polygons...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// loop until there are no polygons left</span>
<span style="color: #006600; font-style: italic;">// or there are still items in the cluster array</span>
<span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>polygonsLeft <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">||</span> clusterIndices.<span style="color: #660066;">length</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// if there are no items in the next row...</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>currentRow.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// if there are items in the cluster index array</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>clusterIndices.<span style="color: #660066;">length</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// log the creation of the cluster</span>
			logmessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Creating cluster with &quot;</span><span style="color: #339933;">+</span>clusterIndices.<span style="color: #660066;">length</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; elements...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// create a new mesh containing all of these polygons</span>
			<span style="color: #006600; font-style: italic;">// first, create an array containing all pointindices</span>
			<span style="color: #006600; font-style: italic;">// and two additional point hash arrays</span>
			<span style="color: #006600; font-style: italic;">// of which one is used to determine if a point is already used</span>
			<span style="color: #006600; font-style: italic;">// and the second one is used to map the original index to the new one</span>
			<span style="color: #003366; font-weight: bold;">var</span> pointIndices <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> pointUsed <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> pointNewIndex <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>clusterIndices.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> poly <span style="color: #339933;">=</span> polygons.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span>clusterIndices<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> pointIndexArray <span style="color: #339933;">=</span> poly.<span style="color: #660066;">points</span>.<span style="color: #660066;">indexArray</span>.<span style="color: #660066;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//(VBS to JScript conversion)</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// loop through all points and check if it they are used</span>
				<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>pointIndexArray.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #003366; font-weight: bold;">var</span> index <span style="color: #339933;">=</span> pointIndexArray<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
					<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>pointUsed<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
					<span style="color: #009900;">&#123;</span>
						pointNewIndex<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> pointIndices.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
						pointIndices.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						pointUsed<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>	
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// now, create the array of the positions of all points of this island</span>
			<span style="color: #003366; font-weight: bold;">var</span> islandPos <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>pointIndices.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> point <span style="color: #339933;">=</span> geo.<span style="color: #660066;">points</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span>pointIndices<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				islandPos.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>point.<span style="color: #660066;">position</span>.<span style="color: #660066;">x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				islandPos.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>point.<span style="color: #660066;">position</span>.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				islandPos.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>point.<span style="color: #660066;">position</span>.<span style="color: #660066;">z</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// now we need to create the description of the polygons</span>
			<span style="color: #003366; font-weight: bold;">var</span> islandPoly <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #006600; font-style: italic;">// for that we loop over the polygons of the island again</span>
			<span style="color: #006600; font-style: italic;">// and record their pointindices, well, actually the remapped</span>
			<span style="color: #006600; font-style: italic;">// indices (given by the pointNewIndex hash array)</span>
			<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>clusterIndices.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> poly <span style="color: #339933;">=</span> polygons.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span>clusterIndices<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> pointIndexArray <span style="color: #339933;">=</span> poly.<span style="color: #660066;">points</span>.<span style="color: #660066;">indexArray</span>.<span style="color: #660066;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//(VBS to JScript conversion)</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// first add the number of points of this polygon </span>
				<span style="color: #006600; font-style: italic;">// to this polygon's description</span>
				islandPoly.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>pointIndexArray.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// now add the remapped index for each point to the description</span>
				<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>pointIndexArray.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #003366; font-weight: bold;">var</span> index <span style="color: #339933;">=</span> pointIndexArray<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
					islandPoly.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>pointNewIndex<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			obj.<span style="color: #660066;">Parent</span>.<span style="color: #660066;">AddPolygonMesh</span><span style="color: #009900;">&#40;</span>islandPos<span style="color: #339933;">,</span>islandPoly<span style="color: #339933;">,</span>obj.<span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;_island&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			clusterIndices <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// if there are no polyons left we can stop now</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>polygonsLeft <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// loop over the polygons and try to find </span>
		<span style="color: #006600; font-style: italic;">// one which hasn't been visited yet</span>
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>polygons.<span style="color: #660066;">Count</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>polygonVisited<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				currentRow.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				clusterIndices.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				polygonVisited<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
				polygonsLeft<span style="color: #339933;">--;</span>
				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// define the nextrow after the current row</span>
	<span style="color: #003366; font-weight: bold;">var</span> nextRow <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// loop over all items in the current row</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>currentRow.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> polygon <span style="color: #339933;">=</span> polygons.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span>currentRow<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> neighbors <span style="color: #339933;">=</span> polygon.<span style="color: #660066;">NeighborPolygons</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// loop through all of the neighbors and add them to the nextRow</span>
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>neighbors.<span style="color: #660066;">count</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// check if the neighbor has been</span>
			<span style="color: #006600; font-style: italic;">// visited yet</span>
			<span style="color: #003366; font-weight: bold;">var</span> neighbor <span style="color: #339933;">=</span> neighbors.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>polygonVisited<span style="color: #009900;">&#91;</span>neighbor<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// add the neighbor to the next row</span>
				nextRow.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>neighbor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				clusterIndices.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>neighbor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				polygonVisited<span style="color: #009900;">&#91;</span>neighbor<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
				polygonsLeft<span style="color: #339933;">--;</span>
				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	currentRow <span style="color: #339933;">=</span> nextRow<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
selection.<span style="color: #660066;">SetAsText</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;_island*&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<img src="http://www.softimageblog.com/?ak_action=api_record_view&id=260&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.softimageblog.com/archives/260/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Steven Caron, OBJ Files, Sexy Bits and Waste</title>
		<link>http://www.softimageblog.com/archives/249#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=steven-caron-obj-files-sexy-bits-and-waste</link>
		<comments>http://www.softimageblog.com/archives/249#comments</comments>
		<pubDate>Sat, 01 Mar 2008 20:25:45 +0000</pubDate>
		<dc:creator>Patrick Boucher</dc:creator>
				<category><![CDATA[Bablings and Ramblings]]></category>
		<category><![CDATA[Modeling]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.xsi-blog.com/archives/249</guid>
		<description><![CDATA[What do all these things have in common?
The weird wirings in my brain. That&#8217;s what.
Back to the beginning
At the end of last October Steven Caron sent me a plugin he wrote that allows an XSI user to drag and drop .obj files into the interface and have them import automatically according to settings in a [...]]]></description>
			<content:encoded><![CDATA[<p>What do all these things have in common?</p>
<p>The weird wirings in my brain. That&#8217;s what.</p>
<p><strong>Back to the beginning</strong></p>
<p>At the end of last October Steven Caron sent me a <a href="http://www.xsi-blog.com/userContent/scaron/ObjDnD.xsiaddon">plugin</a> he wrote that allows an XSI user to drag and drop .obj files into the interface and have them import automatically according to settings in a custom preference. I was a bit swamped at the time putting up the infrastructure for a new VFX department at my new workplace. And then I totally forgot about it&#8230; Sorry Steven.</p>
<p>This past week Steven politely reminded me of my omission so I ran back to my email archives and installed it. Neat piece of work.</p>
<p>I didn&#8217;t want to just post up the tool as I don&#8217;t really see this blog being about tool distribution but about how said tools work and the neat tricks they use. With that philosophy in mind I cracked open Steven&#8217;s tool to dig out some of its sexier bits and maybe demystify them for the audience.</p>
<p>To my surprise there weren&#8217;t any sexy bits. Please don&#8217;t get me wrong, the tool works superbly, how much more intuitive can you get than drag and drop. The tool is also extremely well written, concise and straightforward. There just aren&#8217;t any weird tricks or convoluted syntax or things that would generally have you scratching your head.</p>
<p>Then I noticed this in his version trapping code (yes folks, the drag and drop event is new in XSI 6.5):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span> tXSIVersion<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">6</span>:
    xsiPrint<span style="color: black;">&#40;</span> xsi.<span style="color: black;">Version</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot; doesn't support the 'siOnDragAndDrop' event!&quot;</span> <span style="color: black;">&#41;</span>
    xsi.<span style="color: black;">UnloadPlugin</span><span style="color: black;">&#40;</span> in_reg.<span style="color: black;">name</span>, <span style="color: #008000;">True</span> <span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span> tXSIVersion<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">5</span>:
        xsiPrint<span style="color: black;">&#40;</span> xsi.<span style="color: black;">Version</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot; doesn't support the 'siOnDragAndDrop' event!&quot;</span> <span style="color: black;">&#41;</span>
        xsi.<span style="color: black;">UnloadPlugin</span><span style="color: black;">&#40;</span> in_reg.<span style="color: black;">name</span>, <span style="color: #008000;">True</span> <span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span></pre></td></tr></table></div>

<p><span id="more-249"></span><b>About the waste part</b></p>
<p>Bare with me&#8230;</p>
<p>Last week I was reading a really neat <a href="http://www.wired.com/techbiz/it/magazine/16-03/ff_free">article</a> in <a href="http://www.wired.com">Wired</a> by Chris Anderson entitled <i>&#8220;Free! Why $0.00 Is the Future of Business&#8221;</i>. I invite you to read it, especially the section beginning on page two: <i>Waste and Waste Again</i>.</p>
<p>&#8230;So if in the seventies you had to be really tight with your algorithms and optimizations because cpu cycles were so scarce, today I guess we can really waste them. Just the other day I setup an 8 core MacPro with BootCamp, XP x64 and XSI. Whatever I did to the machine, I swear, I could hear it yawn because it was bored.</p>
<p>Which brings me back to Steven&#8217;s code. Could it have been written like so:</p>
</pre>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>27
28
29
30
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span> tXSIVersion<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">6</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span> tXSIVersion<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">5</span>:
    xsiPrint<span style="color: black;">&#40;</span> xsi.<span style="color: black;">Version</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot; doesn't support the 'siOnDragAndDrop' event!&quot;</span> <span style="color: black;">&#41;</span>
    xsi.<span style="color: black;">UnloadPlugin</span><span style="color: black;">&#40;</span> in_reg.<span style="color: black;">name</span>, <span style="color: #008000;">True</span> <span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span></pre></td></tr></table></div>

<p>Is it more or less readable by a programmer?<br />
It probably executes in a few cycles less but is it worth it?<br />
Python is interpreted so if the parser goes through this version quicker, is it worty of any mention?<br />
Is it more maintainable?</p>
<p>This example is really simple but in a bigger project, or with more complex cases, should we really be worrying if today transistors and cpu cycles are so cheap as to not even matter anymore</p>
<p>When I'm stuck on a piece of code or when I hit that time of the afternoon where most of my energy is diverted to digesting, I'll often reread my code and tighten it, remove redundancy, put in functions instead of copying a few lines in two locations, consolidate <i>if</i> statements, etc... One of my colleagues used to tell me that <i>"Premature optimization is the root of all evil."</i></p>
<p>Honestly, if I look back at a good proportion of the code I have written, it either executes in a blink of an eye on today's computers or it sits there waiting 90% of the time for user interaction. Should we, as technical XSI users, XSI scripters or TDs, even worry about optimization from a performance standpoint or a maintainability standpoint? What can be considered a good optimization and what should be considered a bad one?</p>
<p>And now that you're really wondering what I'm rambling about, I'll shut up and let you <a href="http://www.xsi-blog.com/userContent/scaron/ObjDnD.xsiaddon">download Steven's addon</a>.</p>
<p>Have a good weekend.</pre>
<img src="http://www.softimageblog.com/?ak_action=api_record_view&id=249&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.softimageblog.com/archives/249/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Developing a modeling script &#8211; Start to finish</title>
		<link>http://www.softimageblog.com/archives/93#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=developing-a-modeling-script-start-to-finish</link>
		<comments>http://www.softimageblog.com/archives/93#comments</comments>
		<pubDate>Sat, 18 Mar 2006 16:39:11 +0000</pubDate>
		<dc:creator>Helge Mathee</dc:creator>
				<category><![CDATA[JScript]]></category>
		<category><![CDATA[Modeling]]></category>
		<category><![CDATA[Programming / Scripting]]></category>

		<guid isPermaLink="false">http://www.xsi-blog.com/?p=93</guid>
		<description><![CDATA[Typical problems when developing a modeling script - revisited.]]></description>
			<content:encoded><![CDATA[<p>Hey all.</p>
<p>In this article I want to discuss the full development process of a custom modeling tool, called &#8220;EdgeExtrudePro&#8221;. </p>
<p>What you need:</p>
<p>I going to describe the full process, so be warned, it is going to be a long trip. The article is meant for the experienced scripted as well as the start-up scripting TD, but to follow the descriptions, you&#8217;ll have to know at least the fundamentals of JScript, Arrays (the multiple usages of them, look-up tables etc) and some basic vector math. I am going to explain the math used for this tool briefly, but this is not a math primer. ;-)</p>
<p>What you will get out of it:</p>
<p>This article will get you an understanding of selection checking and user-error handling. Moreover you can learn some of the basic relationships inside of 3D geometry, such as edge &#8211; polygon adjacency, polygon-island boundaries, edge-to-edge connections and some more.</p>
<p>Ok &#8211; so let&#8217;s start.</p>
<p><span id="more-93"></span>The name of the tool was not my idea, so I am just going to keep it that way. What the tool does really, is using a curve to cut a hole into geometry. Here&#8217;s a link to the original tool (For Maxon&#8217;s Cinema 4D) <a href="http://www.vreel-3d.de/plugins/EEPro/EElinks.html">http://www.vreel-3d.de/plugins/EEPro/EElinks.html</a></p>
<p>Here&#8217;s an animated gif from the webpage above, demonstrating the functionality. We are focusing on the part with the red circle.</p>
<p><img id="image145" src="/userContent/upload/2007/01/eeani.gif" alt="eeani.gif" /></p>
<p>Looking the animated gif, I can see the following steps to reach our goal:</p>
<ul>
<li>Get a polygonmesh and a curve</li>
<li>Put the curve onto the surface of the mesh</li>
<li>Delete all polygons &#8220;below&#8221; the curve&#8217;s shape</li>
<li>Extrude the resulting new boundary to fit the curve</li>
</ul>
<p>What&#8217;s really important when developing a custom tool, is to focus on what really has to be done. As you don&#8217;t know all of the steps before you start working on them, it is still good to partition the huge task into smaller tasks, and partition them again, until you have a task you can easily solve. For now, I am just going to rephrase our tasks a little bit.</p>
<ul>
<li>Check the selection for a polygonmesh and a nurbscurvelist</li>
<li>ShrinkWrap the nurbscurvelist onto the polygonmesh.</li>
<li>Delete all polygons &#8220;below&#8221; the nurbscurvelist&#8217;s shape</li>
<li>Extrude the resulting new boundary to fit the curve.</li>
</ul>
<p>Ok &#8211; sounds better. Let&#8217;s start with the first one.</p>
<p><strong>1. Check the selection for a polygonmesh and a nurbscurvelist</strong></p>
<p>I don&#8217;t want to use picking sessions or anything similar right now, I will simply force the user to have two objects selected. Moreover, there are certain conditions I want to be checked. They look like this:</p>
<ul>
<li>The number of objects selected has to be 2</li>
<li>The first object has to be a polygonmesh</li>
<li>The second object has to be a nurbscurvelist</li>
<li>The nurbscurvelist has to be closed</li>
<li>The nurbscurvelist has to contain only one curve</li>
</ul>
<p>Allright, putting all of this into a simple JScript function, it looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// checks if the selection is correct</span>
<span style="color: #003366; font-weight: bold;">function</span> js_checkSelection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// check if we have two objects</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>selection.<span style="color: #660066;">count</span><span style="color: #339933;">!=</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		logmessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please select a polygonmesh and a curvelist!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// check if the first one is a polygonmesh</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>selection<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">type</span><span style="color: #339933;">!=</span><span style="color: #3366CC;">&quot;polymsh&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		logmessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The first object you selected is not a polygonmesh!&quot;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// check if the second object is a curvelist</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>selection<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">type</span><span style="color: #339933;">!=</span><span style="color: #3366CC;">&quot;crvlist&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		logmessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The second object you selected is not a curvelist!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// check if the curve is closed</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>selection<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ActivePrimitive</span>.<span style="color: #660066;">Geometry</span>.<span style="color: #660066;">Closed</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		logmessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The curve you selected is not closed!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// check if the curvelist contains only one curve</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>selection<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ActivePrimitive</span>.<span style="color: #660066;">Geometry</span>.<span style="color: #660066;">Curves</span>.<span style="color: #660066;">count</span><span style="color: #339933;">!=</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		logmessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The curve you selected contains more or less curves than 1!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// if everything went well - just return true</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Allright &#8211; let&#8217;s go to number 2.</p>
<p><strong>2. ShrinkWrap the nurbscurvelist onto the polygonmesh.</strong></p>
<p>For the projection I am just going to use XSI&#8217;s ShrinkWrap. There&#8217;s the option to implement the projection myself, but I try to rely on existing functionality as much as possible &#8211; to speed up the implementation process. Just ShrinkWrapping is not going to do the full job though. So these are the considerations:</p>
<ul>
<li>The two objects have to be in the same reference space</li>
<li>After projection / shrinking the curve onto the surface we want to freeze it, because we might change the topology the curve was shrinked onto</li>
</ul>
<p>The only thing I really did is manually shrink-wrapping a curve onto a polygonmesh, copy &#038; pasting the code out of the script editor history and adding some more steps. Here&#8217;s my shrinking function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//apply the shrinkwrap</span>
<span style="color: #003366; font-weight: bold;">function</span> js_applyShrinkWrap<span style="color: #009900;">&#40;</span>curve<span style="color: #339933;">,</span>mesh<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// if the curve is not parented to the mesh yet</span>
	<span style="color: #006600; font-style: italic;">// parent it in... </span>
	<span style="color: #006600; font-style: italic;">// we have to do this to simplify the reference frames</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>curve.<span style="color: #660066;">parent</span>.<span style="color: #660066;">fullname</span><span style="color: #339933;">!=</span>mesh.<span style="color: #660066;">fullname</span><span style="color: #009900;">&#41;</span>
		ParentObj<span style="color: #009900;">&#40;</span>mesh<span style="color: #339933;">,</span>curve<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ResetTransform<span style="color: #009900;">&#40;</span>curve<span style="color: #339933;">,</span> siCtr<span style="color: #339933;">,</span> siSRT<span style="color: #339933;">,</span> siXYZ<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> op <span style="color: #339933;">=</span> ApplyOp<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ShrinkWrap&quot;</span><span style="color: #339933;">,</span> curve<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;;&quot;</span><span style="color: #339933;">+</span>mesh<span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> siPersistentOperation<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	SetValue<span style="color: #009900;">&#40;</span>op<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;.proj&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	FreezeObj<span style="color: #009900;">&#40;</span>op<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>3. Delete all polygons &#8220;below&#8221; the nurbscurvelist&#8217;s shape</strong></p>
<p>Okay &#8211; so now this is where it gets complicated. I am going to explain how we find what needs to be deleted by partitioning the task again:</p>
<ul>
<li>Find all edges which are crossing the curve</li>
<li>Find all adjacent polygons to these edges</li>
<li>Find the two boundaries of this resulting polygon island (inner and outer boundary)</li>
<li>Find out which boundary is shorter and call it the inner boundary</li>
<li>Get all of the polygons inside of the inner boundary + the polygons from the crossing edges</li>
<li>Delete them</li>
</ul>
<p>I need a function to figure out if an edge crosses a curve. This seems to be a hard one, it&#8217;s not really though. Let&#8217;s look at it:</p>
<p>To find out if an edge is crossing a curve, we need to find out if the two points of the edge are on the &#8220;opposite&#8221; sides of the curve. If they are, the edge crosses the curve. To do that, I will find the closest points on the curve for the two edge-points, find their center, and the center&#8217;s point on the curve. Then I take the tangent of the curve at this position and build a plane which cuts the edge in 3D, or not. If it cuts the edge, it crosses the curve.</p>
<p><img id="image146" src="/userContent/upload/2007/01/ee_pro_03.jpg" alt="ee_pro_03.jpg" /></p>
<p>In the illustration, the blue line is the curve, the green grid marks the plane used for the intersection calculation and the red line is an example edge.</p>
<p>And here&#8217;s the javascript function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// check if an edge crosses a curve</span>
<span style="color: #003366; font-weight: bold;">function</span> js_doesEdgeCrossCurve<span style="color: #009900;">&#40;</span>edge<span style="color: #339933;">,</span>curve<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// get the two positions</span>
	<span style="color: #003366; font-weight: bold;">var</span> edgePos1 <span style="color: #339933;">=</span> edge.<span style="color: #660066;">points</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">position</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> edgePos2 <span style="color: #339933;">=</span> edge.<span style="color: #660066;">points</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">position</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// get the U value of the closest points on the curve</span>
	<span style="color: #003366; font-weight: bold;">var</span> edgeU1 <span style="color: #339933;">=</span> curve.<span style="color: #660066;">GetClosestCurvePosition2</span><span style="color: #009900;">&#40;</span>edgePos1<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>	
	<span style="color: #003366; font-weight: bold;">var</span> edgeU2 <span style="color: #339933;">=</span> curve.<span style="color: #660066;">GetClosestCurvePosition2</span><span style="color: #009900;">&#40;</span>edgePos2<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>	
&nbsp;
	<span style="color: #006600; font-style: italic;">// get the two positions on the curve of the closest points	</span>
	<span style="color: #003366; font-weight: bold;">var</span> edgeOnCurve1 <span style="color: #339933;">=</span> curve.<span style="color: #660066;">curves</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">EvaluatePosition</span><span style="color: #009900;">&#40;</span>edgeU1<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> edgeOnCurve2 <span style="color: #339933;">=</span> curve.<span style="color: #660066;">curves</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">EvaluatePosition</span><span style="color: #009900;">&#40;</span>edgeU2<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// get the position in the center of the two edge on curves</span>
	<span style="color: #003366; font-weight: bold;">var</span> edgePos <span style="color: #339933;">=</span> XSIMath.<span style="color: #660066;">CreateVector3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	edgePos.<span style="color: #660066;">Add</span><span style="color: #009900;">&#40;</span>edgeOnCurve1<span style="color: #339933;">,</span>edgeOnCurve2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	edgePos.<span style="color: #660066;">ScaleInPlace</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0.5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// get the U value of the closest point on the curve to this one</span>
	<span style="color: #003366; font-weight: bold;">var</span> edgeU <span style="color: #339933;">=</span> curve.<span style="color: #660066;">GetClosestCurvePosition2</span><span style="color: #009900;">&#40;</span>edgePos<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>	
	<span style="color: #006600; font-style: italic;">// get the position of that point</span>
	<span style="color: #003366; font-weight: bold;">var</span> edgeOnCurve <span style="color: #339933;">=</span> curve.<span style="color: #660066;">curves</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">EvaluatePosition</span><span style="color: #009900;">&#40;</span>edgeU<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// get the tangent of the curve at that position</span>
	<span style="color: #003366; font-weight: bold;">var</span> tangent <span style="color: #339933;">=</span> curve.<span style="color: #660066;">curves</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">EvaluatePosition</span><span style="color: #009900;">&#40;</span>edgeU<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// get the U value of the closest point on the curve to this one</span>
	<span style="color: #003366; font-weight: bold;">var</span> normal <span style="color: #339933;">=</span> XSIMath.<span style="color: #660066;">CreateVector3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// make the normal the connection of the first point</span>
	<span style="color: #006600; font-style: italic;">// and the position on the curve</span>
	normal.<span style="color: #660066;">Sub</span><span style="color: #009900;">&#40;</span>edgePos1<span style="color: #339933;">,</span>edgeOnCurve<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// cross this result with the tangent to retrieve the bi-normal</span>
	normal.<span style="color: #660066;">Cross</span><span style="color: #009900;">&#40;</span>normal<span style="color: #339933;">,</span>tangent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	normal.<span style="color: #660066;">NormalizeInPlace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// cross this result with the tangent again to retrieve the normal</span>
	normal.<span style="color: #660066;">Cross</span><span style="color: #009900;">&#40;</span>normal<span style="color: #339933;">,</span>tangent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	normal.<span style="color: #660066;">NormalizeInPlace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// define two connection vectors</span>
	<span style="color: #003366; font-weight: bold;">var</span> con1 <span style="color: #339933;">=</span> XSIMath.<span style="color: #660066;">CreateVector3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> con2 <span style="color: #339933;">=</span> XSIMath.<span style="color: #660066;">CreateVector3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// the first is the connection between the edgepos1 and the position on the curve</span>
	<span style="color: #006600; font-style: italic;">// resp. for the second</span>
	con1.<span style="color: #660066;">Sub</span><span style="color: #009900;">&#40;</span>edgePos1<span style="color: #339933;">,</span>edgeOnCurve<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	con2.<span style="color: #660066;">Sub</span><span style="color: #009900;">&#40;</span>edgePos2<span style="color: #339933;">,</span>edgeOnCurve<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// get the distance of the two connection vectors to the plane defined by the normal</span>
	<span style="color: #003366; font-weight: bold;">var</span> dist1 <span style="color: #339933;">=</span> con1.<span style="color: #660066;">Dot</span><span style="color: #009900;">&#40;</span>normal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #003366; font-weight: bold;">var</span> dist2 <span style="color: #339933;">=</span> con2.<span style="color: #660066;">Dot</span><span style="color: #009900;">&#40;</span>normal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
	<span style="color: #006600; font-style: italic;">// if the points are on different sides of the plane</span>
	<span style="color: #006600; font-style: italic;">// (if one distance is larger and the other one less than 0)	</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>dist1<span style="color: #339933;">&gt;=</span><span style="color: #CC0000;">0</span> <span style="color: #339933;">&amp;&amp;</span> dist2<span style="color: #339933;">&lt;</span> <span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span>
		<span style="color: #009900;">&#40;</span>dist1<span style="color: #339933;">&lt;=</span><span style="color: #CC0000;">0</span> <span style="color: #339933;">&amp;&amp;</span> dist2<span style="color: #339933;">&gt;=</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// the edge crosses the curve</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// nope - it doesn't cross</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To find all of the edges, I create a helper function which simply calles <i>js_doesEdgeCrossCurve</i> a couple of times, like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns an array of indices of the edge crossing the curve</span>
<span style="color: #003366; font-weight: bold;">function</span> js_getAllCrossingEdges<span style="color: #009900;">&#40;</span>meshObj<span style="color: #339933;">,</span>curveObj<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// define an array for all of the edges crossing the curve</span>
	<span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// get the geometry of both objects</span>
	<span style="color: #003366; font-weight: bold;">var</span> mesh <span style="color: #339933;">=</span> meshObj.<span style="color: #660066;">ActivePrimitive</span>.<span style="color: #660066;">Geometry</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> curve <span style="color: #339933;">=</span> curveObj.<span style="color: #660066;">ActivePrimitive</span>.<span style="color: #660066;">Geometry</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// loop through</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>mesh .<span style="color: #660066;">edges</span>.<span style="color: #660066;">count</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// get the edge</span>
		<span style="color: #003366; font-weight: bold;">var</span> edge <span style="color: #339933;">=</span> mesh.<span style="color: #660066;">edges</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #006600; font-style: italic;">// if it crosses, put in the result array</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>js_doesEdgeCrossCurve<span style="color: #009900;">&#40;</span>edge<span style="color: #339933;">,</span>curve<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			result<span style="color: #009900;">&#91;</span>result.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Result:</p>
<p><img id="image147" src="/userContent/upload/2007/01/ee_pro_05_b.jpg" alt="ee_pro_05_b.jpg" /></p>
<p>Now as we have all edges crossing the curve, we need to find all of the adjacent polygons. As adjacency is a simple task, I am not going to use XSI&#8217;s command for selecting adjacent components, but implement a JScript function to do the job. It is simply looping over all edges and finding all polygons adjacent to each edge.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// gets all adjacent polygons of an edge-index-array</span>
<span style="color: #003366; font-weight: bold;">function</span> js_getAdjacentPolygons<span style="color: #009900;">&#40;</span>meshObj<span style="color: #339933;">,</span>indices<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// get the geometry of the meshObj</span>
	<span style="color: #003366; font-weight: bold;">var</span> mesh <span style="color: #339933;">=</span> meshObj.<span style="color: #660066;">ActivePrimitive</span>.<span style="color: #660066;">Geometry</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// an array which is used as a hash-table</span>
	<span style="color: #006600; font-style: italic;">// for all of the polyons used</span>
	<span style="color: #006600; font-style: italic;">// example: if polyUsed[4] == true, that means</span>
	<span style="color: #006600; font-style: italic;">// the polygon with the index 4 is adjacent</span>
	<span style="color: #003366; font-weight: bold;">var</span> polyUsed <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// loop through all edge indices	</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>indices .<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// get the edge</span>
		<span style="color: #003366; font-weight: bold;">var</span> edge <span style="color: #339933;">=</span> mesh.<span style="color: #660066;">edges</span><span style="color: #009900;">&#40;</span>indices<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// get the collection of neighborpolygons of this edge</span>
		<span style="color: #006600; font-style: italic;">// note: an edge can have only one polygon linked to it</span>
		<span style="color: #006600; font-style: italic;">// that means it is a boundary edge... if that happens,</span>
		<span style="color: #006600; font-style: italic;">// just return false to tell the calling function that</span>
		<span style="color: #006600; font-style: italic;">// we have to stop now.</span>
		<span style="color: #003366; font-weight: bold;">var</span> nbPolies <span style="color: #339933;">=</span> edge.<span style="color: #660066;">NeighborPolygons</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>nbPolies.<span style="color: #660066;">count</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			logmessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;We cannot cut a boundary!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&lt;</span>nbPolies.<span style="color: #660066;">count</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			polyUsed<span style="color: #009900;">&#91;</span>nbPolies<span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// define the result array</span>
	<span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> index <span style="color: #000066; font-weight: bold;">in</span> polyUsed<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		result<span style="color: #009900;">&#91;</span>result.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> index<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Result:</p>
<p><img id="image148" src="/userContent/upload/2007/01/ee_pro_06_b.jpg" alt="ee_pro_06_b.jpg" /></p>
<p>To find the two boundaries and determine which one is shorter, I loop through all edges of the polygons I just got from the adjacency function and count their occurences. If I find an edge which is used only once, it is a boundary of the polygon-island. To find the two different boundaries, I start with one edge, mark it as non-boundary and find the next connected edge which is a boundary. I repeat that step until I reach the edge I started with. Those edges all together are one boundary (either the inner or the outer one &#8211; I don&#8217;t know that yet). Now I can just take all of the edges and define them as &#8220;the other&#8221; boundary. By comparing the number of elements in both boundaries, I can find the shorter one and define it as &#8220;the inner boundary&#8221;.</p>
<p>First &#8211; I need a function which tells me if two edges are connected or not:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns true if two edges are connected</span>
<span style="color: #003366; font-weight: bold;">function</span> js_areEdgesConnected<span style="color: #009900;">&#40;</span>edge1<span style="color: #339933;">,</span>edge2<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// if the edges are the same, we don't call them &quot;connected&quot;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>edge1.<span style="color: #660066;">index</span><span style="color: #339933;">==</span>edge2.<span style="color: #660066;">index</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// compare all points of the one edge with</span>
	<span style="color: #006600; font-style: italic;">// all points of the other one</span>
	<span style="color: #006600; font-style: italic;">// if there is any match, the edges are connected</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>
		<span style="color: #009900;">&#40;</span>edge1.<span style="color: #660066;">points</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #339933;">==</span>edge2.<span style="color: #660066;">points</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span>
		<span style="color: #009900;">&#40;</span>edge1.<span style="color: #660066;">points</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #339933;">==</span>edge2.<span style="color: #660066;">points</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span>
		<span style="color: #009900;">&#40;</span>edge1.<span style="color: #660066;">points</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #339933;">==</span>edge2.<span style="color: #660066;">points</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span>
		<span style="color: #009900;">&#40;</span>edge1.<span style="color: #660066;">points</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #339933;">==</span>edge2.<span style="color: #660066;">points</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Second &#8211; I need a function which does what I described above: Find the shorter (inner boundary) and its elements:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns the shortest edge boundary of a given polygon index array</span>
<span style="color: #003366; font-weight: bold;">function</span> js_getShortestEdgeBoundary<span style="color: #009900;">&#40;</span>meshObj<span style="color: #339933;">,</span>indices<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// get the geometry of the mesh</span>
	<span style="color: #003366; font-weight: bold;">var</span> mesh <span style="color: #339933;">=</span> meshObj.<span style="color: #660066;">ActivePrimitive</span>.<span style="color: #660066;">Geometry</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// this is a hash-table which counts the usage of edges</span>
	<span style="color: #006600; font-style: italic;">// if the counter is one, the edge is a boundary(!)</span>
	<span style="color: #003366; font-weight: bold;">var</span> edgeCount <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// loop through all given polygons by index</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>indices .<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// get the polygon (called facet here)</span>
		<span style="color: #003366; font-weight: bold;">var</span> facet <span style="color: #339933;">=</span> mesh.<span style="color: #660066;">facets</span><span style="color: #009900;">&#40;</span>indices<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// get the polygons edges</span>
		<span style="color: #003366; font-weight: bold;">var</span> edges <span style="color: #339933;">=</span> facet.<span style="color: #660066;">edges</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&lt;</span>edges.<span style="color: #660066;">count</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// put the index of the edge into the edgeCount array</span>
			<span style="color: #006600; font-style: italic;">// if it is not there yet, otherwise increment the number there</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>edgeCount<span style="color: #009900;">&#91;</span>edges<span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
				edgeCount<span style="color: #009900;">&#91;</span>edges<span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">else</span>
				edgeCount<span style="color: #009900;">&#91;</span>edges<span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// define two arrays for the boundaries</span>
	<span style="color: #003366; font-weight: bold;">var</span> boundary1 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> boundary2 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// now we start by putting the first boundary edge into the boundary1 array</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> index <span style="color: #000066; font-weight: bold;">in</span> edgeCount<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// if the edge is a boundary</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>edgeCount<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// put the edge as the first element in the boundary1 array </span>
			boundary1<span style="color: #009900;">&#91;</span>boundary1.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> index<span style="color: #339933;">;</span>
			<span style="color: #006600; font-style: italic;">// set the edge to be nothing / so it looks like it is not a boundary anymore</span>
			<span style="color: #006600; font-style: italic;">// because we want to use it only once</span>
			edgeCount<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
			<span style="color: #006600; font-style: italic;">// and leave the loop afterwards...</span>
			<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// check again if we have an element</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>boundary1.<span style="color: #660066;">length</span><span style="color: #339933;">!=</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		logmessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;This should never happen... something really bad.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// now we try to add edges to the boundary until we hit the start again</span>
	<span style="color: #006600; font-style: italic;">// we definitively have to leave if we have more edges than exist</span>
	<span style="color: #006600; font-style: italic;">// this is to make sure we don't loop infinitely for some reason</span>
	<span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>boundary1.<span style="color: #660066;">length</span><span style="color: #339933;">&lt;</span>mesh.<span style="color: #660066;">edges</span>.<span style="color: #660066;">count</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// get the last edge in the boundary</span>
		<span style="color: #003366; font-weight: bold;">var</span> lastEdge <span style="color: #339933;">=</span> mesh.<span style="color: #660066;">edges</span><span style="color: #009900;">&#40;</span>boundary1<span style="color: #009900;">&#91;</span>boundary1.<span style="color: #660066;">length</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// check if the boundary is alreay closed</span>
		<span style="color: #006600; font-style: italic;">// the shortest possible boundary has 3 edges,</span>
		<span style="color: #006600; font-style: italic;">// so only check if we have at least 3</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>boundary1.<span style="color: #660066;">length</span><span style="color: #339933;">&gt;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// the first edge in the boundary</span>
			<span style="color: #003366; font-weight: bold;">var</span> firstEdge <span style="color: #339933;">=</span> mesh.<span style="color: #660066;">edges</span><span style="color: #009900;">&#40;</span>boundary1<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #006600; font-style: italic;">// if the edges are connected, the boundary is closed.</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>js_areEdgesConnected<span style="color: #009900;">&#40;</span>firstEdge<span style="color: #339933;">,</span>lastEdge<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// yeah - closed - we leave this loop.</span>
				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// try to find the next edge which is connected to the last one</span>
		<span style="color: #006600; font-style: italic;">// in the boundary... init it with -1, so we can check later if we really found one</span>
		<span style="color: #003366; font-weight: bold;">var</span> newIndex <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// loop through all edges we counted</span>
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> index <span style="color: #000066; font-weight: bold;">in</span> edgeCount<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// if the edge is a boundary </span>
			<span style="color: #006600; font-style: italic;">// note that we set used edges to 0, so we don't use them more than once</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>edgeCount<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// get this edge</span>
				<span style="color: #003366; font-weight: bold;">var</span> currentEdge <span style="color: #339933;">=</span> mesh.<span style="color: #660066;">edges</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// if these two edges are connected, we add it to the boundary</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>js_areEdgesConnected<span style="color: #009900;">&#40;</span>currentEdge<span style="color: #339933;">,</span>lastEdge<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #006600; font-style: italic;">// set the newindex to the current index</span>
					newIndex <span style="color: #339933;">=</span> index<span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #006600; font-style: italic;">// and leave this loop!</span>
					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// if the newIndex is still -1, we didn't find a new edge,</span>
		<span style="color: #006600; font-style: italic;">// which is really bad - so we leave the whole function and give up</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newIndex<span style="color: #339933;">==-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			logmessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Something bad happened. I give up.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// add the newIndex to the boundary</span>
		boundary1<span style="color: #009900;">&#91;</span>boundary1.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> newIndex<span style="color: #339933;">;</span>
		<span style="color: #006600; font-style: italic;">// remove it from the edgeCount array,</span>
		<span style="color: #006600; font-style: italic;">// so we don't use it again</span>
		edgeCount<span style="color: #009900;">&#91;</span>newIndex<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// so now we have the first boundary - the other boundary simply</span>
	<span style="color: #006600; font-style: italic;">// consists of all of the boundary edges left, so:</span>
	<span style="color: #006600; font-style: italic;">// loop through the edgeCount array and get all edges with count1</span>
	<span style="color: #006600; font-style: italic;">// and put them in boundary2</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> index <span style="color: #000066; font-weight: bold;">in</span> edgeCount<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// is it a boundary?</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>edgeCount<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			boundary2<span style="color: #009900;">&#91;</span>boundary2.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> index<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// do a last check if both boundaries are valid</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>boundary1.<span style="color: #660066;">length</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #CC0000;">3</span> <span style="color: #339933;">||</span> boundary2.<span style="color: #660066;">length</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		logmessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The boundaries are too short - that's bad - I give up&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// so now we know both boundaries, we check which one contains less edges</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>boundary1.<span style="color: #660066;">length</span><span style="color: #339933;">&lt;</span>boundary2 .<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">return</span> boundary1<span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">else</span>
		<span style="color: #000066; font-weight: bold;">return</span> boundary2<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Result:</p>
<p><img id="image149" src="/userContent/upload/2007/01/ee_pro_08_b.jpg" alt="ee_pro_08_b.jpg" /></p>
<p>Now we have the polygons crossing the curve and the inner boundary. Another function will now get all polygons to be deleted. It basically does a &#8220;grow selection&#8221;, but with the following rule: We can only grow the selection over edges which are on the inner boundary. That way we get all polygons &#8220;inside&#8221; the boundary &#8211; not the ones outside of the polygons crossing the curve.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// adds all polygons inside of a polygonselection to a given array</span>
<span style="color: #006600; font-style: italic;">// by using a given edgeboundary</span>
<span style="color: #003366; font-weight: bold;">function</span> js_addAllInsidePolygons<span style="color: #009900;">&#40;</span>meshObj<span style="color: #339933;">,</span>polyIndices<span style="color: #339933;">,</span>edgeIndices<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// get the geometry of the meshObj</span>
	<span style="color: #003366; font-weight: bold;">var</span> mesh <span style="color: #339933;">=</span> meshObj.<span style="color: #660066;">ActivePrimitive</span>.<span style="color: #660066;">Geometry</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// put all polygons in a &quot;used&quot; hashtable</span>
	<span style="color: #003366; font-weight: bold;">var</span> polyUsed <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>polyindices .<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		polyUsed<span style="color: #009900;">&#91;</span>polyIndices<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// put all the edges in a &quot;used&quot; hashtable</span>
	<span style="color: #003366; font-weight: bold;">var</span> edgeUsed <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>edgeIndices.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		edgeUsed<span style="color: #009900;">&#91;</span>edgeIndices<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// now loop through all polygons and</span>
	<span style="color: #006600; font-style: italic;">// find all neighbor polygons, but(!)</span>
	<span style="color: #006600; font-style: italic;">// only the one which are connected by edges</span>
	<span style="color: #006600; font-style: italic;">// edgeIndices array</span>
	<span style="color: #006600; font-style: italic;">// so we get only the ones on the right side</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>polyIndices.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// get the polygon (called facet)</span>
		<span style="color: #003366; font-weight: bold;">var</span> facet <span style="color: #339933;">=</span> mesh.<span style="color: #660066;">facets</span><span style="color: #009900;">&#40;</span>polyIndices<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// get the edges of the polygon</span>
		<span style="color: #003366; font-weight: bold;">var</span> edges <span style="color: #339933;">=</span> facet.<span style="color: #660066;">edges</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// loop through all edges and checked if they are used</span>
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&lt;</span>edges.<span style="color: #660066;">count</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>edgeUsed<span style="color: #009900;">&#91;</span>edges<span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// get the two polygons of the edge</span>
				<span style="color: #003366; font-weight: bold;">var</span> nbFacets <span style="color: #339933;">=</span> edges<span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">neighborPolygons</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// if the edge has more than one polygon</span>
				<span style="color: #006600; font-style: italic;">// (if not - we only counted it, otherwise how would we</span>
				<span style="color: #006600; font-style: italic;">// be here, huh?)</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>nbFacets.<span style="color: #660066;">count</span><span style="color: #339933;">&gt;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #006600; font-style: italic;">// for each nb-facet</span>
					<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> l<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>l<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>l<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
					<span style="color: #009900;">&#123;</span>
						<span style="color: #006600; font-style: italic;">// if the facet is not used yet</span>
						<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>polyUsed<span style="color: #009900;">&#91;</span>nbFacets<span style="color: #009900;">&#40;</span>l<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
						<span style="color: #009900;">&#123;</span>
							<span style="color: #006600; font-style: italic;">// put it as used and add it to the polyIndices array</span>
							polyUsed<span style="color: #009900;">&#91;</span>nbFacets<span style="color: #009900;">&#40;</span>l<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
							polyIndices<span style="color: #009900;">&#91;</span>polyIndices.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> nbFacets<span style="color: #009900;">&#40;</span>l<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #339933;">;</span>
&nbsp;
							<span style="color: #006600; font-style: italic;">// afterwards put all edges of the new polygon into the edgeUsed array</span>
							<span style="color: #003366; font-weight: bold;">var</span> nbFacetEdges <span style="color: #339933;">=</span> nbFacets<span style="color: #009900;">&#40;</span>l<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">edges</span><span style="color: #339933;">;</span>
							<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>k<span style="color: #339933;">&lt;</span>nbfacetedges .<span style="color: #660066;">count</span><span style="color: #339933;">;</span>k<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
							<span style="color: #009900;">&#123;</span>
								edgeUsed<span style="color: #009900;">&#91;</span>nbFacetEdges<span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
							<span style="color: #009900;">&#125;</span>
						<span style="color: #009900;">&#125;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// now get all used polygons and return them as an array</span>
	<span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> index <span style="color: #000066; font-weight: bold;">in</span> polyUsed<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		result<span style="color: #009900;">&#91;</span>result.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> index<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Result:</p>
<p><img id="image150" src="/userContent/upload/2007/01/ee_pro_09_b.jpg" alt="ee_pro_09_b.jpg" /></p>
<p><strong>4. Move the resulting new points onto the curve</strong></p>
<p>The extrude is going to be done by the main function, more about that later. For now &#8211; I just write a function which takes all of the points above a specific index and moves them onto the curve by closest point. For this it is really important that we froze the curve earlier, just in case we need to delete the polygons we shrunk onto.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// moves all points of a mesh starting with the given index onto the curve</span>
<span style="color: #003366; font-weight: bold;">function</span> js_movePointsOntoCurve<span style="color: #009900;">&#40;</span>meshObj<span style="color: #339933;">,</span>curveObj<span style="color: #339933;">,</span>startIndex<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// get the geometries of the objects</span>
	<span style="color: #003366; font-weight: bold;">var</span> mesh <span style="color: #339933;">=</span> meshObj.<span style="color: #660066;">ActivePrimitive</span>.<span style="color: #660066;">Geometry</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> curve <span style="color: #339933;">=</span> curveObj.<span style="color: #660066;">ActivePrimitive</span>.<span style="color: #660066;">Geometry</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// loop through all points starting with the given index</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span>startIndex<span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>mesh .<span style="color: #660066;">points</span>.<span style="color: #660066;">count</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> oldPos <span style="color: #339933;">=</span> mesh.<span style="color: #660066;">points</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">position</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// get the U value of the closest point on the curve</span>
		<span style="color: #003366; font-weight: bold;">var</span> newPos <span style="color: #339933;">=</span> curve.<span style="color: #660066;">GetClosestCurvePosition2</span><span style="color: #009900;">&#40;</span>oldPos<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>	
&nbsp;
		<span style="color: #006600; font-style: italic;">// now move the point</span>
		Translate<span style="color: #009900;">&#40;</span>meshObj<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;.pnt[&quot;</span><span style="color: #339933;">+</span>i<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;]&quot;</span><span style="color: #339933;">,</span> newPos.<span style="color: #660066;">x</span><span style="color: #339933;">,</span>newPos.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>newPos.<span style="color: #660066;">z</span><span style="color: #339933;">,</span> siAbsolute<span style="color: #339933;">,</span> siParent<span style="color: #339933;">,</span> siObj<span style="color: #339933;">,</span> siXYZ<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To finish up the tool, we end by creating a main function which calls all of our helper functions one after another.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// the main function to run the tool</span>
<span style="color: #003366; font-weight: bold;">function</span> js_main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// check the selection</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>js_checkSelection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// define our variables (we can because the selection was checked before)</span>
	<span style="color: #003366; font-weight: bold;">var</span> mesh <span style="color: #339933;">=</span> selection<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> curve <span style="color: #339933;">=</span> selection<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// apply the shrink wrap...</span>
	js_applyShrinkWrap<span style="color: #009900;">&#40;</span>curve<span style="color: #339933;">,</span>mesh<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// get the crossing edges..</span>
	<span style="color: #003366; font-weight: bold;">var</span> edgesCrossing <span style="color: #339933;">=</span> js_getAllCrossingEdges<span style="color: #009900;">&#40;</span>mesh<span style="color: #339933;">,</span>curve<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>edgesCrossing.<span style="color: #660066;">length</span><span style="color: #339933;">==</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		logmessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;There are no crossing edges!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// get the adjacent polygons</span>
	<span style="color: #003366; font-weight: bold;">var</span> poliesOnCurve <span style="color: #339933;">=</span> js_getAdjacentPolygons<span style="color: #009900;">&#40;</span>mesh<span style="color: #339933;">,</span>edgesCrossing<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>poliesOnCurve<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// something didn't work - we have to abort.</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// now get the shortest edge boundary</span>
	<span style="color: #003366; font-weight: bold;">var</span> edgeBoundary <span style="color: #339933;">=</span> js_getShortestEdgeBoundary<span style="color: #009900;">&#40;</span>mesh<span style="color: #339933;">,</span>poliesOnCurve<span style="color: #009900;">&#41;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// now get all polygons inside that boundary as well</span>
	<span style="color: #003366; font-weight: bold;">var</span> polygonsToDelete <span style="color: #339933;">=</span> js_addAllInsidePolygons<span style="color: #009900;">&#40;</span>mesh<span style="color: #339933;">,</span>poliesOnCurve<span style="color: #339933;">,</span>edgeBoundary<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// select the adjacent edges of the polygons</span>
	SelectAdjacent<span style="color: #009900;">&#40;</span>mesh<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;.poly[&quot;</span><span style="color: #339933;">+</span>polygonsToDelete<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;]&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Edge&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// delete all polygons... finally something's happenin' !</span>
	ApplyTopoOp<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;DeleteComponent&quot;</span><span style="color: #339933;">,</span> mesh<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;.poly[&quot;</span><span style="color: #339933;">+</span>polygonsToDelete<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;]&quot;</span><span style="color: #339933;">,</span> siUnspecified<span style="color: #339933;">,</span> siPersistentOperation<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// now we have the edges still selected, as we selected the adjacent ones before,</span>
	<span style="color: #006600; font-style: italic;">// what we do now is remember the pointcount - as the points we have to move are the</span>
	<span style="color: #006600; font-style: italic;">// new ones after the extrude operation</span>
	<span style="color: #003366; font-weight: bold;">var</span> pointCount <span style="color: #339933;">=</span> mesh.<span style="color: #660066;">ActivePrimitive</span>.<span style="color: #660066;">Geometry</span>.<span style="color: #660066;">Points</span>.<span style="color: #660066;">Count</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// now duplicate the edges (if we don't specify anything it is going to be the selection</span>
	DuplicateMeshComponent<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> siPersistentOperation<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// now move all points on the mesh onto the curve starting with the given index</span>
	js_movePointsOntoCurve<span style="color: #009900;">&#40;</span>mesh<span style="color: #339933;">,</span>curve<span style="color: #339933;">,</span>pointCount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Result:</p>
<p><img id="image151" src="http://www.xsi-blog.com/userContent/upload/2007/01/ee_pro_11_b.jpg" alt="ee_pro_11_b.jpg" /></p>
<p><strong>Ressources</strong></p>
<p>Here&#8217;s the final script: <a id="p152" href="/userContent/upload/2007/01/edgeextrudepro_v07.zip">Edge Extrude Pro</a><br />
Moreover, here is a camtasia capture of working with it: <a href="/userContent/hmathee/modelingScript/edgeExtrudePro.avi">Camtasia Demo</a> (Techsmith, 9 MB)</p>
<p>cheers!</mesh></pre>
<p></nbfacetedges></polyindices></pre>
<p></boundary2></indices></pre>
<p></indices></pre>
<p></mesh></pre>
<img src="http://www.softimageblog.com/?ak_action=api_record_view&id=93&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.softimageblog.com/archives/93/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
<enclosure url="http://217.160.138.15/helge/rigtut/edgeExtrudePro.avi" length="9064960" type="video/x-msvideo" />
		</item>
		<item>
		<title>What is Softimage Scaling Anyway?</title>
		<link>http://www.softimageblog.com/archives/84#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=what-is-softimage-scaling-anyway</link>
		<comments>http://www.softimageblog.com/archives/84#comments</comments>
		<pubDate>Tue, 31 Jan 2006 03:10:05 +0000</pubDate>
		<dc:creator>Brent McPherson</dc:creator>
				<category><![CDATA[Modeling]]></category>
		<category><![CDATA[User Interface]]></category>

		<guid isPermaLink="false">http://www.xsi-blog.com/?p=84</guid>
		<description><![CDATA[You may not be aware that in XSI scaling is handled differently than in other 3D software packages or perhaps you have encountered situations where scaling a hierarchy did not quite give you the results you were expecting. In this article I crack open the mysteries surrounding Softimage scaling.]]></description>
			<content:encoded><![CDATA[<p>You may not be aware that in XSI scaling is handled differently than in other 3D software packages or perhaps you have encountered situations where scaling a hierarchy did not quite give you the results you were expecting.</p>
<p>In this article I&#8217;m going to crack open the mysteries surrounding Softimage scaling. You might find this useful if you are exporting rigs out of XSI or trying to integrate XSI into an existing pipeline. At the very least you might be able to use this knowledge to impress the newbies at work.</p>
<p><span id="more-84"></span>There are two types of transformation schemes that XSI supports: Softimage scaling (also affectionately known as SI scaling) and classical scaling (which is a term we invented to refer to the way everyone else handles scaling).</p>
<p>SI scaling was invented at Softimage and it was designed to avoid shearing. Shearing happens when an object is scaled in a direction that does not line up with one of its local axes. One characteristic of shearing is that the object&#8217;s local axes may no longer be perpendicular. Shearing can also affect your mesh normals in undesirable ways.</p>
<p><img src="/userContent/upload/shearing.jpg" /></p>
<p>So, if shearing is generally undesirable then why don&#8217;t we see other software taking a similar approach? Well for starters classical scaling more closely matches the matrix-stack transformation model used by the graphics hardware and it is simpler to implement and understand. Another reason is that even though shearing can occur with classical scaling it is fairly easy to avoid in practice.</p>
<p>In fact, under many circumstances SI scaling and classical scaling may yield the exact same results which is one reason why SI scaling is not more well known. In fact, the only situation where SI scaling differs from classical scaling is when you apply different scale factors to one or more axes on a 3D object with children *and* the local axes of the children do not line up with the parent&#8217;s local axes. If you stick to uniform scaling or the parent and children&#8217;s local axes line up or you only apply non uniform scaling to 3D objects without children then the math works out the same.</p>
<p>Time to get down and dirty with the implementation details of SI scaling. I&#8217;m going to try and keep this discussion simple even though transformations in XSI are anything but simple. The local  transform is chock full of buttons and sliders that let you modify scale orientation, pivots, constraints, neutral poses and a bunch of other stuff I&#8217;m too lazy to look up. So, I&#8217;m just going to gloss over all that stuff and pretend that our transformation is made up of a scale, a rotation and a translation. After all those are the main parameters animators deal with and they are the only ones that show up in the transform panel. (so I do have some justification for my simplification) You can think of all the other parameters as optional accessories that hang off the main transform.</p>
<p>The first thing to note is that in both schemes the local transform is defined exactly the same. SI scaling only kicks in when 3D objects are parented in a hierarchy. In both schemes our local matrix is just formed by converting our scaling, rotation and translations to matrices (using the standard 4&#215;4 matrix formulas) and multiplying all three matrices together.</p>
<pre>M      = M      x M      x M
 local    scale    rot      trans</pre>
<p>Now, here is where classical scaling really shines. To get a global matrix for a 3D object A which is parented under another 3D object B we simply multiply together the local matrix and the parent&#8217;s global matrix.</p>
<pre>A       = A      x  B
 global    local     global</pre>
<p>Nice and simple. If B has a parent C we expand this formula again. (Note: if an object doesn&#8221;t have any parents then the global matrix is the same as the local matrix)</p>
<pre>A         A      x  B      x C      x  and so on...
 global =  local     local    local</pre>
<p>SI scaling takes a different approach by constraining the scale factors to always happen around an object&#8217;s local axes. Therefore, shearing cannot occur by definition. So, what this means is that, when dealing with a hierarchy, all the scaling matrices are combined together first followed by all the rotation matrices and finally all the global translations.</p>
<p>One other thing to note is that SI scaling is not a simple matrix product (like classical scaling) since it requires us to handle translations in a slightly different manner. In SI scaling a global matrix is formed as follows.</p>
<pre>A       = A      x B      x A    x B    x AB
 global    scale    scale    rot    rot    trans</pre>
<p>You can see how the in SI scaling the scale/rotation/translation components are combined separately. The only strange thing in this equation is the AB translation matrix and this is formed by taking the translation component of the following matrix product. (which is just a fancy way of getting the translation in parent space by transforming it by the parent&#8217;s local matrix)</p>
<pre>AB      = 4th row of matrix | A       x  B      |
 trans                      |   trans     local |</pre>
<p>And if we add another node to the hierarchy we get the following.</p>
<pre>A       = A      x B      x C      x A    x B    x C    x ABC
 global    scale    scale    scale    rot    rot    rot    trans

ABC      = 4th row of matrix | AB       x  C      |
 trans                       |  trans       local |</pre>
<p>Whew! That was a lot of trouble to avoid shearing. [1]</p>
<p>Most users won&#8217;t need to concern themselves with SI scaling versus classical scaling especially if they are doing everything in XSI and just outputting rendered frames. However, I would still like to give you a practical example of where you might want to use classical scaling instead of SI scaling. In the example below I have created a brick wall by translating and rotating individual (identical) bricks and parenting them under a null. Now, if I want to squish the entire wall by scaling the (null) parent I will get very different results depending on whether I am using SI or classical scaling. In this situation classical scaling is more useful since we do not want the bricks to scale along their local axes. (which point in different directions)</p>
<p><img src="/userContent/upload/bricks.jpg" /></p>
<p>The good news is that, in XSI, you can mix and match SI scaling and classical scaling to fit your needs. Perhaps you are more comfortable with classical scaling because you cut your teeth on a different 3D software package.</p>
<p>First, if you want to make classical scaling the default in XSI, there is a preference to activate classical scaling globally under File &gt; Preferences &gt; General &gt; Use Classical Scaling for Newly Created Objects.</p>
<p>If however you just want to use classical scaling on a few objects you simply need to 1) select all the objects in your hierarchy 2) hit Ctrl+K to inspect their local transforms and 3) turn off &#8220;Hierarchical (Softimage) Scaling&#8221; parameter. (which is found under the &#8220;Scaling&#8221; tab)</p>
<p>If you are interested in learning more about SI scaling and transforms you might also want to also check out Michael Isner&#8217;s <a href="http://www.isner.com/Transform/IsnerTransformManip_04.htm">website</a> where he explores SI scaling from a technical animator/TD perspective. He even has some nifty JScript sample code for dealing with SI scaling that you can incorporate into your own scripts.</p>
<p>Footnotes:</p>
<p>[1] In XSI there is actually a way to shear local transforms by using the scale orientation parameter which changes the axes about which scaling occurs. Oh the irony!</p>
<img src="http://www.softimageblog.com/?ak_action=api_record_view&id=84&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.softimageblog.com/archives/84/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>A View to a Selection</title>
		<link>http://www.softimageblog.com/archives/27#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=a-view-to-a-selection</link>
		<comments>http://www.softimageblog.com/archives/27#comments</comments>
		<pubDate>Tue, 24 May 2005 21:54:53 +0000</pubDate>
		<dc:creator>Patrick Boucher</dc:creator>
				<category><![CDATA[Modeling]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.xsi-blog.com/?p=27</guid>
		<description><![CDATA[So you&#8217;re starting to texture a character and think:
&#8220;It would be nice if I could select all the polygons that are facing the camera&#8230;&#8221;
Okay, so maybe you haven&#8217;t had that reflection but Gabriel Tremblay sure has, and one fateful evening he talked to me about it over a beer. So here it is, from his [...]]]></description>
			<content:encoded><![CDATA[<p>So you&#8217;re starting to texture a character and think:</p>
<p>&#8220;It would be nice if I could select all the polygons that are facing the camera&#8230;&#8221;</p>
<p>Okay, so maybe you haven&#8217;t had that reflection but Gabriel Tremblay sure has, and one fateful evening he talked to me about it over a beer. So here it is, from his brain to your workspace with a little detour through my keyboard: <a href="/distrib/PB_selPolyFromCam/latest/PB_selPolyFromCam.zip">PB_selPolyFromCam</a>.</p>
<p>The idea behind this tool is to select polygons on polygonal objects as a planar or camera projection would, if it could. If the polygon is facing the camera of choice and within a threshold angle from the camera&#8217;s axis&#8230; BAM! It&#8217;s selected. Weeelllll, not so fast, I&#8217;d have to write the tool in C++ and compile it as well as remove the few remaining commands that are in there to gain some additionnal speed. Not to mention my actual algorythim might not be as tight as it could be.</p>
<p>Here&#8217;s a snapshot of the UI to help visualize how the tool works:</p>
<p><img src="/distrib/PB_selPolyFromCam/2005.05.24/ppg1.png" alt="PPG of PB_selPolyFromCam" /></p>
<p>Here is what the above PPG setup looks like on a sphere. The camera doing the selecting is the one in the screenshot.</p>
<p><img src="/distrib/PB_selPolyFromCam/2005.05.24/viewport.png" alt="PPG of PB_selPolyFromCam" /></p>
<p>Because I couldn&#8217;t get to the Selection menu in the Selection pannel, I hooked the tool in the Modify -&gt; PolyMesh section of the Model toolbar. Did someone over at Softimage understand the subtle hint? I&#8217;d like to hook things into the Selection menu of the Selection pannel&#8230; Just driving the point home. Thanks!</p>
<p>Just drop the <a href="/distrib/PB_selPolyFromCam/latest/PB_selPolyFromCam.zip">file</a> in your favorite Application\Plugins location and have a ball! Of course you&#8217;ll also need the latest buzz.xsi library available through the <a href="/?page_id=25">download area</a>.</p>
<p>I hope all you texturers and camera projectors out there enjoy!<br />
As usual, if you find bugs or have comments, <a href="mailto:pboucher@xsi-blog.com">don&#8217;t be shy</a>.</p>
<p>Thanks go out to Gabriel and the guys at Buzz on this one.</p>
<img src="http://www.softimageblog.com/?ak_action=api_record_view&id=27&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.softimageblog.com/archives/27/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
