bgc-c/dev/main.c

187 lines
3.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <geometry.h>
#ifdef _WIN64
#include <windows.h>
#else
#include <time.h>
#endif // _WINDOWS_
SPVersor * allocate_versors(const unsigned int amount)
{
return calloc(amount, sizeof(SPVersor));
}
SPVersor * make_zero_versors(const unsigned int amount)
{
SPVersor * list = allocate_versors(amount);
if (list == 0) {
return 0;
}
for (unsigned int i = 0; i < amount; i++) {
sp_versor_reset(&list[i]);
}
return list;
}
SPVersor * make_random_versors(const unsigned int amount)
{
SPVersor * list = allocate_versors(amount);
if (list == 0) {
return 0;
}
for (unsigned int i = 0; i < amount; i++) {
sp_versor_set(
(2.0f * rand()) / RAND_MAX - 1.0f,
(2.0f * rand()) / RAND_MAX - 1.0f,
(2.0f * rand()) / RAND_MAX - 1.0f,
(2.0f * rand()) / RAND_MAX - 1.0f,
&list[i]
);
}
return list;
}
void print_versor(const SPVersor * versor)
{
printf("(%f, %f, %f, %f) / %e\n", versor->_s0, versor->_x1, versor->_x2, versor->_x3, versor->_corrector - 1.0f);
}
/*/
int main()
{
const unsigned int amount = 1000000;
#ifdef _WIN64
ULONGLONG now;
now = GetTickCount64();
srand((unsigned int)(now & 0xfffffff));
#else
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
srand((unsigned int)(now.tv_nsec & 0xfffffff));
#endif // _WIN64
SPVersor * versors1 = make_random_versors(amount);
if (versors1 == 0) {
printf("Cannot allocate memory for versors1");
return 0;
}
SPVersor * versors2 = make_random_versors(amount);
if (versors2 == 0) {
printf("Cannot allocate memory for versors2");
free(versors1);
return 0;
}
SPVersor * results = make_zero_versors(amount);
if (results == 0) {
printf("Cannot allocate memory for results");
free(versors2);
free(versors1);
return 0;
}
#ifdef _WIN64
ULONGLONG start, end;
start = GetTickCount64();
#else
struct timespec start, end;
clock_gettime(CLOCK_REALTIME, &start);
#endif // _WIN64
for (int j = 0; j < 1000; j++) {
for (unsigned int i = 0; i < amount; i++) {
sp_versor_combine2(&versors1[i], &versors2[i], &results[i]);
}
}
#ifdef _WIN64
end = GetTickCount64();
printf("Time: %lld\n", end - start);
#else
clock_gettime(CLOCK_REALTIME, &end);
printf("Time: %lf\n", (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_nsec - start.tv_nsec) * 0.000001);
#endif // _WIN64
print_versor(versors1 + 10);
print_versor(versors2 + 10);
print_versor(results + 10);
free(results);
free(versors2);
free(versors1);
return 0;
}
/*/
void print_matrix(const DPMatrix3x3* matrix)
{
printf("(%lf, %lf, %lf)\n", matrix->r1c1, matrix->r1c2, matrix->r1c3);
printf("(%lf, %lf, %lf)\n", matrix->r2c1, matrix->r2c2, matrix->r2c3);
printf("(%lf, %lf, %lf)\n\n", matrix->r3c1, matrix->r3c2, matrix->r3c3);
}
/*
int main()
{
DPMatrix3x3 m1, m2, check;
dp_matrix3x3_set_row1(1, 3, 5, &m1);
dp_matrix3x3_set_row2(2, 2, -1, &m1);
dp_matrix3x3_set_row3(2, 0, 4, &m1);
printf("Initial matrix:\n");
print_matrix(&m1);
if (!dp_matrix3x3_make_inverted(&m1, &m2)) {
printf("Cannot get the inverted matrix.\n");
return 0;
}
printf("Inverted matrix:\n");
print_matrix(&m2);
dp_matrix3x3_matrix_product(&m1, &m2, &check);
printf("m1 * m2:\n");
print_matrix(&check);
dp_matrix3x3_matrix_product(&m2, &m1, &check);
printf("m2 * m1:\n");
print_matrix(&check);
return 0;
}
*/
int main()
{
SPVector2 vector;
sp_vector2_set(0, 0, &vector);
printf("SPVector2(%f, %f), module = %d\n",
vector.x1,
vector.x2,
sp_vector2_is_zero(&vector)
);
return 0;
}