forked from cemyuksel/cyCodeBase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyHairFile.h
421 lines (349 loc) · 17 KB
/
cyHairFile.h
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// cyCodeBase by Cem Yuksel
// [www.cemyuksel.com]
//-------------------------------------------------------------------------------
//! \file cyHairFile.h
//! \author Cem Yuksel
//!
//! \brief A class for the HAIR file type
//!
//-------------------------------------------------------------------------------
//
// Copyright (c) 2016, Cem Yuksel <[email protected]>
// All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//-------------------------------------------------------------------------------
#ifndef _CY_HAIR_FILE_H_INCLUDED_
#define _CY_HAIR_FILE_H_INCLUDED_
//-------------------------------------------------------------------------------
#include "cyCore.h"
#include <stdio.h>
//-------------------------------------------------------------------------------
_CY_CRT_SECURE_NO_WARNINGS
//-------------------------------------------------------------------------------
namespace cy {
//-------------------------------------------------------------------------------
#define _CY_HAIR_FILE_SEGMENTS_BIT 1
#define _CY_HAIR_FILE_POINTS_BIT 2
#define _CY_HAIR_FILE_THICKNESS_BIT 4
#define _CY_HAIR_FILE_TRANSPARENCY_BIT 8
#define _CY_HAIR_FILE_COLORS_BIT 16
#define _CY_HAIR_FILE_INFO_SIZE 88
// File read errors
#define CY_HAIR_FILE_ERROR_CANT_OPEN_FILE -1 //!< Error code cannot open file
#define CY_HAIR_FILE_ERROR_CANT_READ_HEADER -2 //!< Error code cannot read header
#define CY_HAIR_FILE_ERROR_WRONG_SIGNATURE -3 //!< Error code wrong signature
#define CY_HAIR_FILE_ERROR_READING_SEGMENTS -4 //!< Error code failed reading segments
#define CY_HAIR_FILE_ERROR_READING_POINTS -5 //!< Error code failed reading points
#define CY_HAIR_FILE_ERROR_READING_THICKNESS -6 //!< Error code failed reading thickness
#define CY_HAIR_FILE_ERROR_READING_TRANSPARENCY -7 //!< Error code failed reading transparency
#define CY_HAIR_FILE_ERROR_READING_COLORS -8 //!< Error code failed reading colors
//-------------------------------------------------------------------------------
//! HAIR file class
class HairFile
{
public:
HairFile() : segments(nullptr), points(nullptr), thickness(nullptr), transparency(nullptr), colors(nullptr) { Initialize(); }
~HairFile() { Initialize(); }
//! Hair file header
struct Header
{
char signature[4]; //!< This should be "HAIR"
unsigned int hair_count; //!< number of hair strands
unsigned int point_count; //!< total number of points of all strands
unsigned int arrays; //!< bit array of data in the file
unsigned int d_segments; //!< default number of segments of each strand
float d_thickness; //!< default thickness of hair strands
float d_transparency; //!< default transparency of hair strands
float d_color[3]; //!< default color of hair strands
char info[_CY_HAIR_FILE_INFO_SIZE]; //!< information about the file
};
//////////////////////////////////////////////////////////////////////////
//!@name Constant Data Access Methods
Header const & GetHeader () const { return header; } //!< Use this method to access header data.
unsigned short const * GetSegmentsArray () const { return segments; } //!< Returns segments array (segment count for each hair strand).
float const * GetPointsArray () const { return points; } //!< Returns points array (xyz coordinates of each hair point).
float const * GetThicknessArray () const { return thickness; } //!< Returns thickness array (thickness at each hair point}.
float const * GetTransparencyArray() const { return transparency; } //!< Returns transparency array (transparency at each hair point).
float const * GetColorsArray () const { return colors; } //!< Returns colors array (rgb color at each hair point).
//////////////////////////////////////////////////////////////////////////
//!@name Data Access Methods
unsigned short* GetSegmentsArray () { return segments; } //!< Returns segments array (segment count for each hair strand).
float* GetPointsArray () { return points; } //!< Returns points array (xyz coordinates of each hair point).
float* GetThicknessArray () { return thickness; } //!< Returns thickness array (thickness at each hair point}.
float* GetTransparencyArray() { return transparency; } //!< Returns transparency array (transparency at each hair point).
float* GetColorsArray () { return colors; } //!< Returns colors array (rgb color at each hair point).
//////////////////////////////////////////////////////////////////////////
//!@name Methods for Setting Array Sizes
//! Deletes all arrays and initializes the header data.
void Initialize()
{
if ( segments ) delete [] segments;
if ( points ) delete [] points;
if ( colors ) delete [] colors;
if ( thickness ) delete [] thickness;
if ( transparency ) delete [] transparency;
header.signature[0] = 'H';
header.signature[1] = 'A';
header.signature[2] = 'I';
header.signature[3] = 'R';
header.hair_count = 0;
header.point_count = 0;
header.arrays = 0; // no arrays
header.d_segments = 0;
header.d_thickness = 1.0f;
header.d_transparency = 0.0f;
header.d_color[0] = 1.0f;
header.d_color[1] = 1.0f;
header.d_color[2] = 1.0f;
memset( header.info, '\0', _CY_HAIR_FILE_INFO_SIZE );
}
//! Sets the hair count, re-allocates segments array if necessary.
void SetHairCount( int count )
{
header.hair_count = count;
if ( segments ) {
delete [] segments;
segments = new unsigned short[ header.hair_count ];
}
}
// Sets the point count, re-allocates points, thickness, transparency, and colors arrays if necessary.
void SetPointCount( int count )
{
header.point_count = count;
if ( points ) {
delete [] points;
points = new float[ header.point_count*3 ];
}
if ( thickness ) {
delete [] thickness;
thickness = new float[ header.point_count ];
}
if ( transparency ) {
delete [] transparency;
transparency = new float[ header.point_count ];
}
if ( colors ) {
delete [] colors;
colors = new float[ header.point_count*3 ];
}
}
//! Use this function to allocate/delete arrays.
//! Before you call this method set hair count and point count.
//! Note that a valid HAIR file should always have points array.
void SetArrays( int array_types )
{
header.arrays = array_types;
if ( (header.arrays & _CY_HAIR_FILE_SEGMENTS_BIT ) && !segments ) { segments = new unsigned short[header.hair_count]; }
if ( !(header.arrays & _CY_HAIR_FILE_SEGMENTS_BIT ) && segments ) { delete [] segments; segments=nullptr; }
if ( (header.arrays & _CY_HAIR_FILE_POINTS_BIT ) && !points ) { points = new float[header.point_count*3]; }
if ( !(header.arrays & _CY_HAIR_FILE_POINTS_BIT ) && points ) { delete [] points; points=nullptr; }
if ( (header.arrays & _CY_HAIR_FILE_THICKNESS_BIT ) && !thickness ) { thickness = new float[header.point_count]; }
if ( !(header.arrays & _CY_HAIR_FILE_THICKNESS_BIT ) && thickness ) { delete [] thickness; thickness=nullptr; }
if ( (header.arrays & _CY_HAIR_FILE_TRANSPARENCY_BIT) && !transparency ) { transparency = new float[header.point_count]; }
if ( !(header.arrays & _CY_HAIR_FILE_TRANSPARENCY_BIT) && transparency ) { delete [] transparency; transparency=nullptr; }
if ( (header.arrays & _CY_HAIR_FILE_COLORS_BIT ) && !colors ) { colors = new float[header.point_count*3]; }
if ( !(header.arrays & _CY_HAIR_FILE_COLORS_BIT ) && colors ) { delete [] colors; colors=nullptr; }
}
//! Sets default number of segments for all hair strands, which is used if segments array does not exist.
void SetDefaultSegmentCount( int s ) { header.d_segments = s; }
//! Sets default hair strand thickness, which is used if thickness array does not exist.
void SetDefaultThickness( float t ) { header.d_thickness = t; }
//! Sets default hair strand transparency, which is used if transparency array does not exist.
void SetDefaultTransparency( float t ) { header.d_transparency = t; }
//! Sets default hair color, which is used if color array does not exist.
void SetDefaultColor( float r, float g, float b ) { header.d_color[0]=r; header.d_color[1]=g; header.d_color[2]=b; }
//////////////////////////////////////////////////////////////////////////
//!@name Load and Save Methods
//! Loads hair data from the given HAIR file.
int LoadFromFile( char const *filename )
{
Initialize();
FILE *fp;
fp = fopen( filename, "rb" );
if ( fp == nullptr ) return CY_HAIR_FILE_ERROR_CANT_OPEN_FILE;
// read the header
size_t headread = fread( &header, sizeof(Header), 1, fp );
#define _CY_FAILED_RETURN(errorno) { Initialize(); fclose( fp ); return errorno; }
// Check if header is correctly read
if ( headread < 1 ) _CY_FAILED_RETURN(CY_HAIR_FILE_ERROR_CANT_READ_HEADER);
// Check if this is a hair file
if ( strncmp( header.signature, "HAIR", 4) != 0 ) _CY_FAILED_RETURN(CY_HAIR_FILE_ERROR_WRONG_SIGNATURE);
// Read segments array
if ( header.arrays & _CY_HAIR_FILE_SEGMENTS_BIT ) {
segments = new unsigned short[ header.hair_count ];
size_t readcount = fread( segments, sizeof(unsigned short), header.hair_count, fp );
if ( readcount < header.hair_count ) _CY_FAILED_RETURN(CY_HAIR_FILE_ERROR_READING_SEGMENTS);
}
// Read points array
if ( header.arrays & _CY_HAIR_FILE_POINTS_BIT ) {
points = new float[ header.point_count*3 ];
size_t readcount = fread( points, sizeof(float), header.point_count*3, fp );
if ( readcount < header.point_count*3 ) _CY_FAILED_RETURN(CY_HAIR_FILE_ERROR_READING_POINTS);
}
// Read thickness array
if ( header.arrays & _CY_HAIR_FILE_THICKNESS_BIT ) {
thickness = new float[ header.point_count ];
size_t readcount = fread( thickness, sizeof(float), header.point_count, fp );
if ( readcount < header.point_count ) _CY_FAILED_RETURN(CY_HAIR_FILE_ERROR_READING_THICKNESS);
}
// Read thickness array
if ( header.arrays & _CY_HAIR_FILE_TRANSPARENCY_BIT ) {
transparency = new float[ header.point_count ];
size_t readcount = fread( transparency, sizeof(float), header.point_count, fp );
if ( readcount < header.point_count ) _CY_FAILED_RETURN(CY_HAIR_FILE_ERROR_READING_TRANSPARENCY);
}
// Read colors array
if ( header.arrays & _CY_HAIR_FILE_COLORS_BIT ) {
colors = new float[ header.point_count*3 ];
size_t readcount = fread( colors, sizeof(float), header.point_count*3, fp );
if ( readcount < header.point_count*3 ) _CY_FAILED_RETURN(CY_HAIR_FILE_ERROR_READING_COLORS);
}
fclose( fp );
return header.hair_count;
}
//! Saves hair data to the given HAIR file.
int SaveToFile( char const *filename ) const
{
FILE *fp;
fp = fopen( filename, "wb" );
if ( fp == nullptr ) return -1;
// Write header
fwrite( &header, sizeof(Header), 1, fp );
// Write arrays
if ( header.arrays & _CY_HAIR_FILE_SEGMENTS_BIT ) fwrite( segments, sizeof(unsigned short), header.hair_count, fp );
if ( header.arrays & _CY_HAIR_FILE_POINTS_BIT ) fwrite( points, sizeof(float), header.point_count*3, fp );
if ( header.arrays & _CY_HAIR_FILE_THICKNESS_BIT ) fwrite( thickness, sizeof(float), header.point_count, fp );
if ( header.arrays & _CY_HAIR_FILE_TRANSPARENCY_BIT ) fwrite( transparency, sizeof(float), header.point_count, fp );
if ( header.arrays & _CY_HAIR_FILE_COLORS_BIT ) fwrite( colors, sizeof(float), header.point_count*3, fp );
fclose( fp );
return header.hair_count;
}
//////////////////////////////////////////////////////////////////////////
//!@name Other Methods
//! Fills the given direction array with normalized directions using the points array.
//! Call this function if you need strand directions for shading.
//! The given array dir should be allocated as an array of size 3 times point count.
//! Returns point count, returns zero if fails.
int FillDirectionArray( float *dir )
{
if ( dir==nullptr || header.point_count<=0 || points==nullptr ) return 0;
int p = 0; // point index
for ( unsigned int i=0; i<header.hair_count; i++ ) {
int s = (segments) ? segments[i] : header.d_segments;
if ( s > 1 ) {
// direction at point1
float len0, len1;
ComputeDirection( &dir[(p+1)*3], len0, len1, &points[p*3], &points[(p+1)*3], &points[(p+2)*3] );
// direction at point0
float d0[3];
d0[0] = points[(p+1)*3] - dir[(p+1)*3] *len0*0.3333f - points[p*3];
d0[1] = points[(p+1)*3+1] - dir[(p+1)*3+1]*len0*0.3333f - points[p*3+1];
d0[2] = points[(p+1)*3+2] - dir[(p+1)*3+2]*len0*0.3333f - points[p*3+2];
float d0lensq = d0[0]*d0[0] + d0[1]*d0[1] + d0[2]*d0[2];
float d0len = ( d0lensq > 0 ) ? (float) sqrt(d0lensq) : 1.0f;
dir[p*3] = d0[0] / d0len;
dir[p*3+1] = d0[1] / d0len;
dir[p*3+2] = d0[2] / d0len;
// We computed the first 2 points
p += 2;
// Compute the direction for the rest
for ( int t=2; t<s; t++, p++ ) {
ComputeDirection( &dir[p*3], len0, len1, &points[(p-1)*3], &points[p*3], &points[(p+1)*3] );
}
// direction at the last point
d0[0] = - points[(p-1)*3] + dir[(p-1)*3] *len1*0.3333f + points[p*3];
d0[1] = - points[(p-1)*3+1] + dir[(p-1)*3+1]*len1*0.3333f + points[p*3+1];
d0[2] = - points[(p-1)*3+2] + dir[(p-1)*3+2]*len1*0.3333f + points[p*3+2];
d0lensq = d0[0]*d0[0] + d0[1]*d0[1] + d0[2]*d0[2];
d0len = ( d0lensq > 0 ) ? (float) sqrt(d0lensq) : 1.0f;
dir[p*3] = d0[0] / d0len;
dir[p*3+1] = d0[1] / d0len;
dir[p*3+2] = d0[2] / d0len;
p++;
} else if ( s > 0 ) {
// if it has a single segment
float d0[3];
d0[0] = points[(p+1)*3] - points[p*3];
d0[1] = points[(p+1)*3+1] - points[p*3+1];
d0[2] = points[(p+1)*3+2] - points[p*3+2];
float d0lensq = d0[0]*d0[0] + d0[1]*d0[1] + d0[2]*d0[2];
float d0len = ( d0lensq > 0 ) ? (float) sqrt(d0lensq) : 1.0f;
dir[p*3] = d0[0] / d0len;
dir[p*3+1] = d0[1] / d0len;
dir[p*3+2] = d0[2] / d0len;
dir[(p+1)*3] = dir[p*3];
dir[(p+1)*3+1] = dir[p*3+1];
dir[(p+1)*3+2] = dir[p*3+2];
p += 2;
}
//*/
}
return p;
}
private:
//////////////////////////////////////////////////////////////////////////
//!@name Private Variables and Methods
Header header;
unsigned short *segments;
float *points;
float *thickness;
float *transparency;
float *colors;
// Given point before (p0) and after (p2), computes the direction (d) at p1.
float ComputeDirection( float *d, float &d0len, float &d1len, float const *p0, float const *p1, float const *p2 )
{
// line from p0 to p1
float d0[3];
d0[0] = p1[0] - p0[0];
d0[1] = p1[1] - p0[1];
d0[2] = p1[2] - p0[2];
float d0lensq = d0[0]*d0[0] + d0[1]*d0[1] + d0[2]*d0[2];
d0len = ( d0lensq > 0 ) ? (float) sqrt(d0lensq) : 1.0f;
// line from p1 to p2
float d1[3];
d1[0] = p2[0] - p1[0];
d1[1] = p2[1] - p1[1];
d1[2] = p2[2] - p1[2];
float d1lensq = d1[0]*d1[0] + d1[1]*d1[1] + d1[2]*d1[2];
d1len = ( d1lensq > 0 ) ? (float) sqrt(d1lensq) : 1.0f;
// make sure that d0 and d1 has the same length
d0[0] *= d1len / d0len;
d0[1] *= d1len / d0len;
d0[2] *= d1len / d0len;
// direction at p1
d[0] = d0[0] + d1[0];
d[1] = d0[1] + d1[1];
d[2] = d0[2] + d1[2];
float dlensq = d[0]*d[0] + d[1]*d[1] + d[2]*d[2];
float dlen = ( dlensq > 0 ) ? (float) sqrt(dlensq) : 1.0f;
d[0] /= dlen;
d[1] /= dlen;
d[2] /= dlen;
return d0len;
}
};
//-------------------------------------------------------------------------------
} // namespace cy
//-------------------------------------------------------------------------------
typedef cy::HairFile cyHairFile; //!< HAIR file class
//-------------------------------------------------------------------------------
_CY_CRT_SECURE_RESUME_WARNINGS
#endif