1 //
2 // This source file is part of appleseed.
3 // Visit https://appleseedhq.net/ for additional information and resources.
4 //
5 // This software is released under the MIT license.
6 //
7 // Copyright (c) 2017-2018 Luis Barrancos, The appleseedhq Organization
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26 //
27 
28 #pragma once
29 
compute_id_manifold(int manifold_type,int domain,int seed,string expression,output int hash_id,output color color_id,output float greyscale_id)30 void compute_id_manifold(
31     int manifold_type,
32     int domain,
33     int seed,
34     string expression,
35     output int hash_id,
36     output color color_id,
37     output float greyscale_id)
38 {
39     string manifold_str = "";
40 
41     hash_id = 0;
42     color_id = color(0);
43     greyscale_id = 0.0;
44 
45     if (manifold_type == 0)
46     {
47         getattribute("object:object_name", manifold_str);
48         hash_id = hash(manifold_str);
49     }
50     else if (manifold_type == 1)
51     {
52         getattribute("object:object_instance_name", manifold_str);
53         hash_id= hash(manifold_str);
54     }
55     else if (manifold_type == 2)
56     {
57         getattribute("object:assembly_name", manifold_str);
58         hash_id = hash(manifold_str);
59     }
60     else if (manifold_type == 3)
61     {
62         getattribute("object:assembly_instance_name", manifold_str);
63         hash_id = hash(manifold_str);
64     }
65     else if (manifold_type == 4)
66     {
67         getattribute("object:face_id", hash_id);
68     }
69     else if (expression != "")
70     {
71         if (domain == 0)
72         {
73             getattribute("object:object_name", manifold_str);
74         }
75         else if (domain == 1)
76         {
77             getattribute("object:object_instance_name", manifold_str);
78         }
79         else if (domain == 2)
80         {
81             getattribute("object:assembly_name", manifold_str);
82         }
83         else
84         {
85             getattribute("object:assembly_instance_name", manifold_str);
86         }
87 
88         if (manifold_type == 5)
89         {
90             if (startswith(manifold_str, expression))
91             {
92                 hash_id = hash(seed);
93             }
94         }
95         else if (manifold_type == 6)
96         {
97             if (endswith(manifold_str, expression))
98             {
99                 hash_id = hash(seed);
100             }
101         }
102         else if (regex_search(manifold_str, expression))
103         {
104             hash_id = hash(seed);
105         }
106     }
107 
108     greyscale_id = (float) cellnoise(hash_id);
109     color_id = (color) cellnoise(hash_id);
110 }
111 
rotate2d(float x,float y,float angle_in_degrees,output float rx,output float ry)112 void rotate2d(
113     float x,
114     float y,
115     float angle_in_degrees,
116     output float rx,
117     output float ry)
118 {
119     float angle_rad = radians(angle_in_degrees);
120     float c, s;
121     sincos(angle_rad, s, c);
122     rx = x * c - s * y;
123     ry = x * s + c * y;
124 }
125