From: Rafał Długołęcki Date: Mon, 13 Jan 2014 20:10:49 +0000 (+0100) Subject: Added object position managing functions and structures. X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=46c09ea2689913bb459c5f04439647aa138dc196;p=familia.git Added object position managing functions and structures. --- diff --git a/Makefile.am b/Makefile.am index 187b942..7d30f83 100644 --- a/Makefile.am +++ b/Makefile.am @@ -38,6 +38,8 @@ bin_familia_SOURCES = \ src/storage/storage.c \ src/storage/individual.c \ src/storage/family.c \ + src/storage/positions.c \ + src/math/positions.c \ src/gedcom/familia_gedcom.c \ src/memory_stack.c \ src/gui.c \ diff --git a/doc/model.dia b/doc/model.dia index 5490dcd..f02a738 100644 Binary files a/doc/model.dia and b/doc/model.dia differ diff --git a/src/math/3d.h b/src/math/3d.h new file mode 100644 index 0000000..bc5bfc3 --- /dev/null +++ b/src/math/3d.h @@ -0,0 +1,60 @@ +/**************************************************************************** + * Familia Lignum - Genealogical program * + * Copyright (C) 2011-2012 Rafał Długołęcki * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; version 2 of the License. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * + * * + ****************************************************************************/ + +#ifndef _FAMILIA_MATH_3D_H +#define _FAMILIA_MATH_3D_H + +/** + * + */ +struct position { + /** x coordinate */ + float x; + + /** y coordinate */ + float y; + + /** z coordinate */ + float z; +}; + +struct quaternion { + /** Quaternion angle */ + float w; + + /** Quaternion x coordinate */ + float x; + + /** Quaternion y coordinate */ + float y; + + /** Quaternion z coordinate */ + float z; +}; + +/* +struct matrix quaternion_to_matrix(struct quaternion q); +struct quaternion matrix_to_quaternion(struct matrix4 m); + +struct quaternion slerp(struct quaternion q1, struct quaternion q2, float time); + +struct quaternion quaternion_normalise(struct quaternion q) +*/ + +#endif /* _FAMILIA_MATH_3D_H */ diff --git a/src/math/positions.c b/src/math/positions.c new file mode 100644 index 0000000..4fd47bf --- /dev/null +++ b/src/math/positions.c @@ -0,0 +1,41 @@ +/**************************************************************************** + * Familia Lignum - Genealogical program * + * Copyright (C) 2011-2012 Rafał Długołęcki * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; version 2 of the License. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * + * * + ****************************************************************************/ + +#ifndef _FAMILIA_MATH_POSITIONS_H +#define _FAMILIA_MATH_POSITIONS_H + +#include + +#include "positions.h" +#include "storage/individual.h" +#include "storage/family.h" + +struct position * familia_position_calculate_individual_position(struct familia_individual * individual) +{ +/* TODO: Implement this function*/ + return NULL; +} + +struct position * familia_position_calculate_family_position(struct familia_family * family) +{ +/* TODO: Implement this function*/ + return NULL; +} + +#endif /* _FAMILIA_MATH_3D_H */ diff --git a/src/math/positions.h b/src/math/positions.h new file mode 100644 index 0000000..6db7f3d --- /dev/null +++ b/src/math/positions.h @@ -0,0 +1,42 @@ +/**************************************************************************** + * Familia Lignum - Genealogical program * + * Copyright (C) 2011-2012 Rafał Długołęcki * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; version 2 of the License. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * + * * + ****************************************************************************/ + +#ifndef _FAMILIA_MATH_POSITIONS_H +#define _FAMILIA_MATH_POSITIONS_H + +#include "3d.h" + +#include "storage/individual.h" +#include "storage/family.h" + +/** + * Calculates local position of given individual + * @parameter individual to calculate position + * @return calculated local position of the individual + */ +struct position * familia_position_calculate_individual_position(struct familia_individual * individual); + +/** + * Calculates local position of given family + * @parameter family to calculate position + * @return calculated local position of the family + */ +struct position * familia_position_calculate_family_position(struct familia_family * family); + +#endif /* _FAMILIA_MATH_3D_H */ diff --git a/src/storage/positions.c b/src/storage/positions.c new file mode 100644 index 0000000..b2e977e --- /dev/null +++ b/src/storage/positions.c @@ -0,0 +1,233 @@ +/**************************************************************************** + * Familia Lignum - Genealogical program * + * Copyright (C) 2011-2012 Rafał Długołęcki * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; version 2 of the License. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * + * * + ****************************************************************************/ + +#include +#include + +#include "../debug.h" + +#include "positions.h" + +/** + * Structure holding individual and position pair + */ +struct familia_storage_individual_position { + struct familia_individual *ind; + struct position *pos; +}; + +/** + * Structure holding family and position pair + */ +struct familia_storage_family_position { + struct familia_family *fam; + struct position *pos; +}; + +/** + * Structure holding individual and family positions + */ +struct familia_storage_positions { + struct familia_storage_individual_position **individuals; + + unsigned int individuals_no; + + struct familia_storage_family_position **families; + + unsigned int families_no; +}; + +/** + * Global variable for storing positions of graphic objects. + * + * Example graphic object is an individual or family... + */ +struct familia_storage_positions * storage_positions; + +/** + * Initializes storage for individual and family positions. + */ +void _familia_storage_positions_init() +{ + if (!storage_positions) { + storage_positions = malloc(sizeof(struct familia_storage_positions)); + + if (!storage_positions) { + debug("["__FILE__ "] There was a problem with allocating memory for storage of positions.\n"); + } + else { + storage_positions->individuals_no = 0; + storage_positions->families_no = 0; + } + } +} + +/** + * Frees storage for the positions + */ +void _familia_storage_positions_free() +{ + if (storage_positions) { + if (storage_positions->individuals) { + free(storage_positions->individuals); + } + + if (storage_positions->families) { + free(storage_positions->families); + } + + free(storage_positions); + storage_positions = NULL; + } +} + +/** + * Adds new position for the given individual. + * + * This function should be invoked only internaly and thus is not declared in + * header. Usage without caution can create duplicates in global variable + * storage_positions. Variable storage_positions is not prepared for such duplicates + * and this can provide to errors. + */ +void _familia_positions_add_individual_position(struct familia_individual *individual, struct position *pos) +{ + struct familia_storage_individual_position ** tmp = NULL; + unsigned int alloc_size = sizeof(struct familia_storage_individual_position *); + + int size = (storage_positions->individuals_no + 1); + + tmp = realloc(storage_positions->individuals, size * alloc_size); + + if (tmp) { + storage_positions->individuals = tmp; + storage_positions->individuals[storage_positions->individuals_no]->ind = individual; + storage_positions->individuals[storage_positions->individuals_no]->pos = pos; + storage_positions->individuals_no++; + } + else { + debug("There were problems with allocating memory for storage of individual positions->\n"); + } +} + +/** + * Adds new position for the given family. + * + * This function should be invoked only internaly and thus is not declared in + * header. Usage without caution can create duplicates in global variable + * storage_positions. Variable storage_positions is not prepared for such duplicates + * and this can provide to errors. + */ +void _familia_positions_add_family_position(struct familia_family *family, struct position *pos) +{ + struct familia_storage_family_position ** tmp = NULL; + unsigned int alloc_size = sizeof(struct familia_storage_family_position *); + + int size = (storage_positions->families_no + 1); + + tmp = realloc(storage_positions->families, size * alloc_size); + + if (tmp) { + storage_positions->families = tmp; + storage_positions->families[storage_positions->families_no]->fam = family; + storage_positions->families[storage_positions->families_no]->pos = pos; + storage_positions->families_no++; + } + else { + debug("There were problems with allocating memory for storage of family positions.\n"); + } +} + +void familia_positions_set_individual_position(struct familia_individual *individual, struct position *pos) +{ + struct position * p = NULL; + + p = familia_position_get_individual_position(individual); + + /* If not found create a new one */ + if (p == NULL) { + _familia_positions_add_individual_position(individual, pos); + } +} + +void familia_positions_set_family_position(struct familia_family *family, struct position *pos) +{ + struct position * p = NULL; + + p = familia_position_get_family_position(family); + + /* If not found create a new one */ + if (p == NULL) { + _familia_positions_add_family_position(family, pos); + } +} + +struct position * familia_position_get_individual_position(struct familia_individual *individual) +{ + unsigned int i; + unsigned int id; + + /* + * Uninitialized global. Return error. + */ + if (!storage_positions || !storage_positions->individuals) { + debug("Trying to request individual position, but positions storage is not initialized.\n"); + return NULL; + } + + id = individual->id; + + for (i = 0; i < storage_positions->individuals_no; i++) { + if (storage_positions->individuals[i]->ind->id == id) { + return storage_positions->individuals[i]->pos; + } + } + + /* + * Entry not found for a given individual. Return error. + */ + return NULL; +} + +struct position * familia_position_get_family_position(struct familia_family *family) +{ + unsigned int i; + unsigned int id; + + /* + * Uninitialized global. Return error. + */ + if (!storage_positions || !storage_positions->families) { + debug("Trying to request family position, but positions storage is not initialized.\n"); + return NULL; + } + + id = family->id; + + for (i = 0; i < storage_positions->families_no; i++) { + if (storage_positions->families[i]->fam->id == id) { + return storage_positions->families[i]->pos; + } + } + + /* + * Entry not found for a given individual. Return error. + */ + return NULL; +} + diff --git a/src/storage/positions.h b/src/storage/positions.h new file mode 100644 index 0000000..2d3473e --- /dev/null +++ b/src/storage/positions.h @@ -0,0 +1,56 @@ +/**************************************************************************** + * Familia Lignum - Genealogical program * + * Copyright (C) 2011-2012 Rafał Długołęcki * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; version 2 of the License. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * + * * + ****************************************************************************/ + +#ifndef _FAMILIA_STORAGE_POSITIONS_H +#define _FAMILIA_STORAGE_POSITIONS_H + +#include "../math/3d.h" + +#include "individual.h" +#include "family.h" + +/** + * Sets position for the given individual. + * @parameter individual individual to set the position + * @parameter position position which will be used for individual + */ +void familia_positions_set_individual_position(struct familia_individual *individual, struct position *pos); + +/** + * Sets position for the given family. + * @parameter family family to set the position + * @parameter position position which will be used for family + */ +void familia_positions_set_family_position(struct familia_family *family, struct position *pos); + +/** + * Gets position of the given individual. + * @parameter individual individual for which the position will be get + * @return position of the individual or NULL on error + */ +struct position * familia_position_get_individual_position(struct familia_individual *individual); + +/** + * Gets position of the given family + * @parameter family family for which the position will be get + * @return position of the family or NULL on error + */ +struct position * familia_position_get_family_position(struct familia_family *family); + +#endif /* _FAMILIA_STORAGE_POSITIONS_H */ diff --git a/src/storage/storage.c b/src/storage/storage.c index 9f74f90..272e6af 100644 --- a/src/storage/storage.c +++ b/src/storage/storage.c @@ -6,6 +6,9 @@ struct familia_storage * _familia_storage_current; +extern void _familia_storage_positions_init(); +extern void _familia_storage_positions_free(); + struct familia_storage * familia_storage_get_current() { return _familia_storage_current; @@ -31,6 +34,8 @@ struct familia_storage * familia_storage_new() storage->families = NULL; storage->families_no = 0; + _familia_storage_positions_init(); + return storage; } @@ -57,6 +62,8 @@ void familia_storage_free(struct familia_storage * storage) storage->families = NULL; storage->families_no = 0; } + + _familia_storage_positions_free(); } void familia_storage_add_individual(struct familia_storage * storage, struct familia_individual * individual)