Skip to content

Commit 4fe1211

Browse files
committed
initial commit of existing files
1 parent 89b109e commit 4fe1211

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+7190
-0
lines changed

3rdparty/utils/bmp_save.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <stdio.h>
2+
#include <memory.h>
3+
#include <cstdlib>
4+
5+
#include "bmp_save.h"
6+
7+
int /* O - 0 = success, -1 = failure */
8+
SaveDIBitmap(const char *filename, /* I - File to load */
9+
BITMAPINFO *info, /* I - Bitmap information */
10+
char *bits) /* I - Bitmap data */
11+
{
12+
FILE *fp; /* Open file pointer */
13+
int size; /* Size of file */
14+
unsigned int infosize, /* Size of bitmap info */
15+
bitsize; /* Size of bitmap pixels */
16+
17+
BITMAPFILEHEADER header; /* File header */
18+
19+
20+
/* Try opening the file; use "wb" mode to write this *binary* file. */
21+
if ((fp = fopen(filename, "wb")) == NULL)
22+
return (-1);
23+
24+
/* Figure out the bitmap size */
25+
if (info->bmiHeader.biSizeImage == 0)
26+
bitsize = (info->bmiHeader.biWidth *
27+
info->bmiHeader.biBitCount + 7) / 8 *
28+
abs(info->bmiHeader.biHeight);
29+
else
30+
bitsize = info->bmiHeader.biSizeImage;
31+
32+
/* Figure out the header size */
33+
infosize = sizeof(BITMAPINFOHEADER);
34+
switch (info->bmiHeader.biCompression)
35+
{
36+
case BI_BITFIELDS :
37+
infosize += 12; /* Add 3 RGB doubleword masks */
38+
if (info->bmiHeader.biClrUsed == 0)
39+
break;
40+
case BI_RGB :
41+
if (info->bmiHeader.biBitCount > 8 &&
42+
info->bmiHeader.biClrUsed == 0)
43+
break;
44+
case BI_RLE8 :
45+
case BI_RLE4 :
46+
if (info->bmiHeader.biClrUsed == 0)
47+
infosize += (1 << info->bmiHeader.biBitCount) * 4;
48+
else
49+
infosize += info->bmiHeader.biClrUsed * 4;
50+
break;
51+
}
52+
53+
size = sizeof(BITMAPFILEHEADER) + infosize + bitsize;
54+
55+
/* Write the file header, bitmap information, and bitmap pixel data... */
56+
header.bfType = BF_TYPE; /* 'MB' */
57+
header.bfSize = size;
58+
header.bfReserved1 = 0;
59+
header.bfReserved2 = 0;
60+
header.bfOffBits = sizeof(BITMAPFILEHEADER) + infosize;
61+
62+
if (fwrite(&header, 1, sizeof(BITMAPFILEHEADER), fp) < sizeof(BITMAPFILEHEADER))
63+
{
64+
/* Couldn't write the file header - return... */
65+
fclose(fp);
66+
return (-1);
67+
}
68+
69+
if (fwrite(info, 1, infosize, fp) < infosize)
70+
{
71+
/* Couldn't write the bitmap header - return... */
72+
fclose(fp);
73+
return (-1);
74+
}
75+
76+
if (fwrite(bits, 1, bitsize, fp) < bitsize)
77+
{
78+
/* Couldn't write the bitmap - return... */
79+
fclose(fp);
80+
return (-1);
81+
}
82+
83+
/* OK, everything went fine - return... */
84+
fclose(fp);
85+
return (0);
86+
}

3rdparty/utils/bmp_save.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#ifndef _BMP_SAVE_H_
2+
#define _BMP_SAVE_H_
3+
4+
#include <stdint.h>
5+
6+
typedef struct {
7+
uint16_t bfType; /* Magic identifier */
8+
uint32_t bfSize; /* File size in bytes */
9+
uint16_t bfReserved1, bfReserved2;
10+
uint32_t bfOffBits; /* Offset to image data, bytes */
11+
} BITMAPFILEHEADER;
12+
13+
//http://www.fourcc.org/rgb.php
14+
#define BI_RLE8 0x1
15+
#define BI_RGB 0x0
16+
#define BI_BITFIELDS 0x3
17+
#define BI_RLE4 0x2
18+
# define BF_TYPE 0x4D42 /* "MB" */
19+
20+
typedef struct {
21+
unsigned int size; /* Header size in bytes */
22+
int width,height; /* Width and height of image */
23+
unsigned short int planes; /* Number of colour planes */
24+
unsigned short int bits; /* Bits per pixel */
25+
unsigned int compression; /* Compression type */
26+
unsigned int imagesize; /* Image size in bytes */
27+
int xresolution,yresolution; /* Pixels per meter */
28+
unsigned int ncolours; /* Number of colours */
29+
unsigned int importantcolours; /* Important colours */
30+
} INFOHEADER;
31+
32+
typedef struct tagBITMAPINFOHEADER {
33+
uint32_t biSize;
34+
long biWidth;
35+
long biHeight;
36+
uint16_t biPlanes;
37+
uint16_t biBitCount;
38+
uint32_t biCompression;
39+
uint32_t biSizeImage;
40+
long biXPelsPerMeter;
41+
long biYPelsPerMeter;
42+
uint32_t biClrUsed;
43+
uint32_t biClrImportant;
44+
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
45+
46+
typedef struct tagRGBQUAD {
47+
uint8_t rgbBlue;
48+
uint8_t rgbGreen;
49+
uint8_t rgbRed;
50+
uint8_t rgbReserved;
51+
} RGBQUAD;
52+
53+
typedef struct tagBITMAPINFO {
54+
BITMAPINFOHEADER bmiHeader;
55+
RGBQUAD bmiColors[1];
56+
} BITMAPINFO, *PBITMAPINFO;
57+
58+
59+
int /* O - 0 = success, -1 = failure */
60+
SaveDIBitmap(const char *filename, /* I - File to load */
61+
BITMAPINFO *info, /* I - Bitmap information */
62+
char *bits); /* I - Bitmap data */
63+
64+
#endif // _BMP_SAVE_H_

3rdparty/utils/bvh.cpp

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#include "bvh.h"
2+
#include <stdint.h>
3+
#include <cassert>
4+
5+
// "Insert" a 0 bit after each of the 16 low bits of x
6+
uint32_t Part1By1(uint32_t x)
7+
{
8+
x &= 0x0000ffff; // x = ---- ---- ---- ---- fedc ba98 7654 3210
9+
x = (x ^ (x << 8)) & 0x00ff00ff; // x = ---- ---- fedc ba98 ---- ---- 7654 3210
10+
x = (x ^ (x << 4)) & 0x0f0f0f0f; // x = ---- fedc ---- ba98 ---- 7654 ---- 3210
11+
x = (x ^ (x << 2)) & 0x33333333; // x = --fe --dc --ba --98 --76 --54 --32 --10
12+
x = (x ^ (x << 1)) & 0x55555555; // x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0
13+
return x;
14+
}
15+
16+
// "Insert" two 0 bits after each of the 10 low bits of x
17+
uint32_t Part1By2(uint32_t x)
18+
{
19+
x &= 0x000003ff; // x = ---- ---- ---- ---- ---- --98 7654 3210
20+
x = (x ^ (x << 16)) & 0xff0000ff; // x = ---- --98 ---- ---- ---- ---- 7654 3210
21+
x = (x ^ (x << 8)) & 0x0300f00f; // x = ---- --98 ---- ---- 7654 ---- ---- 3210
22+
x = (x ^ (x << 4)) & 0x030c30c3; // x = ---- --98 ---- 76-- --54 ---- 32-- --10
23+
x = (x ^ (x << 2)) & 0x09249249; // x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
24+
return x;
25+
}
26+
27+
uint32_t EncodeMorton2(uint32_t x, uint32_t y)
28+
{
29+
return (Part1By1(y) << 1) + Part1By1(x);
30+
}
31+
32+
uint32_t EncodeMorton3(uint32_t x, uint32_t y, uint32_t z)
33+
{
34+
return (Part1By2(z) << 2) + (Part1By2(y) << 1) + Part1By2(x);
35+
}
36+
37+
#define MSB_MORTON 29
38+
39+
int classify(uint32_t mc, int i)
40+
{
41+
return 0;
42+
}
43+
44+
BVHNode* create_bvh_node()
45+
{
46+
return new BVHNode();
47+
}
48+
49+
#define MAX_DEPTH 3
50+
51+
52+
void swap(float* radius, float (*pos)[3], int src, int dst)
53+
{
54+
float r = radius[dst];
55+
float x = pos[dst][0];
56+
float y = pos[dst][1];
57+
float z = pos[dst][2];
58+
59+
radius[dst] = radius[src];
60+
pos[dst][0] = pos[src][0];
61+
pos[dst][1] = pos[src][1];
62+
pos[dst][2] = pos[src][2];
63+
64+
radius[dst] = r;
65+
pos[dst][0] = x;
66+
pos[dst][1] = y;
67+
pos[dst][2] = z;
68+
}
69+
70+
void get_sphere_aabb(AABB* paabb, float r, float x, float y, float z)
71+
{
72+
assert(paabb);
73+
paabb->min[0] = x - r;
74+
paabb->max[0] = x + r;
75+
paabb->min[1] = y - r;
76+
paabb->max[1] = y + r;
77+
paabb->min[2] = z - r;
78+
paabb->max[2] = z + r;
79+
}
80+
// one = one U two
81+
void union_aabb(AABB* one, AABB* two)
82+
{
83+
assert(one && two);
84+
85+
one->min[0] = one->min[0] < two->min[0] ? one->min[0] : two->min[0];
86+
one->max[0] = one->max[0] > two->max[0] ? one->max[0] : two->max[0];
87+
one->min[1] = one->min[1] < two->min[1] ? one->min[1] : two->min[1];
88+
one->max[1] = one->max[1] > two->max[1] ? one->max[1] : two->max[1];
89+
one->min[2] = one->min[2] < two->min[2] ? one->min[2] : two->min[2];
90+
one->max[2] = one->max[2] > two->max[2] ? one->max[2] : two->max[2];
91+
}
92+
93+
BVHNode* construct_bvh(BVHNode* pnode, float* radius, float (*pos)[3], int num, int offset, int depth, float* oradius, float (*opos)[3])
94+
{
95+
int left = 0;
96+
int right = 0;
97+
AABB laabb, raabb;
98+
99+
for(int i=offset;i<offset + num;++i)
100+
{
101+
uint32_t morton_code = EncodeMorton3( (uint32_t)(pos[i][0] + 0.5f), (uint32_t)(pos[i][1] + 0.5f), (uint32_t)(pos[i][2] + 0.5f) );
102+
int side = (morton_code & (1<<(MSB_MORTON - depth))) >> (MSB_MORTON - depth);
103+
right += side;
104+
left += (1-side);
105+
//if(side==0 && i > left - 1 - offset)
106+
//{
107+
//swap(radius, pos, i, offset + left -1);
108+
//}
109+
if(side==0)
110+
{
111+
AABB l;
112+
oradius[offset + left-1] = radius[i];
113+
opos[offset + left-1][0] = pos[i][0];
114+
opos[offset + left-1][1] = pos[i][1];
115+
opos[offset + left-1][2] = pos[i][2];
116+
get_sphere_aabb(&l, radius[i], pos[i][0], pos[i][1], pos[i][2]);
117+
union_aabb(&laabb, &l);
118+
}
119+
else
120+
{
121+
AABB r;
122+
oradius[offset + num - right] = radius[i];
123+
opos[offset + num - right][0] = pos[i][0];
124+
opos[offset + num - right][1] = pos[i][1];
125+
opos[offset + num - right][2] = pos[i][2];
126+
get_sphere_aabb(&r, radius[i], pos[i][0], pos[i][1], pos[i][2]);
127+
union_aabb(&raabb, &r);
128+
}
129+
}
130+
131+
if(left > 1)
132+
{
133+
pnode->left_ = create_bvh_node();
134+
pnode->left_->aabb_ = laabb;
135+
construct_bvh(pnode->left_, oradius, opos, left, offset, depth+1, radius, pos);
136+
}
137+
if(right > 1)
138+
{
139+
pnode->right_ = create_bvh_node();
140+
pnode->right_->aabb_ = raabb;
141+
construct_bvh(pnode->right_, oradius, opos, right, offset + left, depth+1, radius, pos);
142+
}
143+
144+
union_aabb(&laabb, &raabb);
145+
pnode->aabb_ = laabb;
146+
147+
return 0;
148+
}

3rdparty/utils/bvh.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef __BVH_H__
2+
#define __BVH_H__
3+
4+
#include <float.h>
5+
6+
struct AABB {
7+
AABB()
8+
{
9+
min[0] = min[1] = min[2] = FLT_MAX;
10+
max[0] = max[1] = max[2] = -FLT_MAX;
11+
}
12+
float min[3];
13+
float max[3];
14+
};
15+
16+
struct BVHNode {
17+
BVHNode():right_(0), left_(0), radius_(0), pos_(0), num_(0) {}
18+
19+
AABB aabb_;
20+
BVHNode* right_;
21+
BVHNode* left_;
22+
23+
float* radius_;
24+
float (*pos_)[3];
25+
int num_;
26+
};
27+
28+
//BVHNode* construct_bvh(float* radius, float (*pos)[3], int num, int offset, int depth);
29+
BVHNode* construct_bvh(BVHNode* pnode, float* radius, float (*pos)[3], int num, int offset, int depth, float* oradius, float (*opos)[3]);
30+
BVHNode* create_bvh_node();
31+
32+
#endif // __BVH_H__

0 commit comments

Comments
 (0)