<commit_before>/*
    Copyright (C) 1996-2008 by <NAME> <www.kyprianidis.com>
    All rights reserved.
    
    This program is free  software: you can redistribute it and/or modify 
    it under the terms of the GNU Lesser General Public License as published 
    by the Free Software Foundation, either version 2.1 of the License, or 
    (at your option) any later version.

    Thisprogram  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 Lesser General Public License for more details.
    
    You should  have received a copy of the GNU Lesser General Public License
    along with  this program; If not, see <http://www.gnu.org/licenses/>. 
*/
#include "lib3ds_impl.h"


/*!
 * Create and return a new node object.
 *
 * The node is returned with an identity matrix. All other fields
 * are zero.
 *
 * \return Lib3dsNode
 */
Lib3dsNode*
lib3ds_node_new(Lib3dsNodeType type) {
    Lib3dsNode *node;
    switch (type) {
        case LIB3DS_NODE_AMBIENT_COLOR: {
            Lib3dsAmbientColorNode *n = (Lib3dsAmbientColorNode*)calloc(sizeof(Lib3dsAmbientColorNode), 1);
            node = (Lib3dsNode*)n;
            strcpy(node->name, "$AMBIENT$");
            n->color_track.type = LIB3DS_TRACK_VECTOR;
            break;
        }

        case LIB3DS_NODE_MESH_INSTANCE: {
            Lib3dsMeshInstanceNode *n = (Lib3dsMeshInstanceNode*)calloc(sizeof(Lib3dsMeshInstanceNode), 1);
            node = (Lib3dsNode*)n;
            strcpy(node->name, "$$$DUMMY");
            n->pos_track.type = LIB3DS_TRACK_VECTOR;
            n->scl_track.type = LIB3DS_TRACK_VECTOR;
            n->rot_track.type = LIB3DS_TRACK_QUAT;
            n->hide_track.type = LIB3DS_TRACK_BOOL;
            break;
        }

        case LIB3DS_NODE_CAMERA: {
            Lib3dsCameraNode *n = (Lib3dsCameraNode*)calloc(sizeof(Lib3dsCameraNode), 1);
            node = (Lib3dsNode*)n;
            n->pos_track.type = LIB3DS_TRACK_VECTOR;
            n->fov_track.type = LIB3DS_TRACK_FLOAT;
            n->roll_track.type = LIB3DS_TRACK_FLOAT;
            break;
        }

        case LIB3DS_NODE_CAMERA_TARGET: {
            Lib3dsTargetNode *n = (Lib3dsTargetNode*)calloc(sizeof(Lib3dsTargetNode), 1);
            node = (Lib3dsNode*)n;
            n->pos_track.type = LIB3DS_TRACK_VECTOR;
            break;
        }

        case LIB3DS_NODE_OMNILIGHT: {
            Lib3dsOmnilightNode *n = (Lib3dsOmnilightNode*)calloc(sizeof(Lib3dsOmnilightNode), 1);
            node = (Lib3dsNode*)n;
            n->pos_track.type = LIB3DS_TRACK_VECTOR;
            n->color_track.type = LIB3DS_TRACK_VECTOR;
            break;
        }

        case LIB3DS_NODE_SPOTLIGHT: {
            Lib3dsSpotlightNode *n = (Lib3dsSpotlightNode*)calloc(sizeof(Lib3dsSpotlightNode), 1);
            node = (Lib3dsNode*)n;
            n->pos_track.type = LIB3DS_TRACK_VECTOR;
            n->color_track.type = LIB3DS_TRACK_VECTOR;
            n->hotspot_track.type = LIB3DS_TRACK_FLOAT;
            n->falloff_track.type = LIB3DS_TRACK_FLOAT;
            n->roll_track.type = LIB3DS_TRACK_FLOAT;
            break;
        }

        case LIB3DS_NODE_SPOTLIGHT_TARGET: {
            Lib3dsTargetNode *n = (Lib3dsTargetNode*)calloc(sizeof(Lib3dsTargetNode), 1);
            node = (Lib3dsNode*)n;
            n->pos_track.type = LIB3DS_TRACK_VECTOR;
            break;
        }

        default:
            assert(0);
            return NULL;
    }

    node->type = type;
    node->node_id = 65535;
    node->user_id = 65535;
    lib3ds_matrix_identity(node->matrix);
    return node;
}


Lib3dsAmbientColorNode* 
lib3ds_node_new_ambient_color(float color0[3]) {
    Lib3dsNode *node;
    Lib3dsAmbientColorNode *n;

    node = lib3ds_node_new(LIB3DS_NODE_AMBIENT_COLOR);

    n = (Lib3dsAmbientColorNode*)node;
    lib3ds_track_resize(&n->color_track, 1);
    if (color0) {
        lib3ds_vector_copy(n->color_track.keys[0].value, color0);
    } else {
        lib3ds_vector_zero(n->color_track.keys[0].value);
    }

    return n;
}


Lib3dsMeshInstanceNode* 
lib3ds_node_new_mesh_instance(Lib3dsMesh *mesh, const char *instance_name, float pos0[3], float scl0[3], float rot0[4]) {
    Lib3dsNode *node;
    Lib3dsMeshInstanceNode *n;
    int i;

    node = lib3ds_node_new(LIB3DS_NODE_MESH_INSTANCE);
    if (mesh) {
        strcpy(node->name, mesh->name);
    } else {
        strcpy(node->name, "$$$DUMMY");
    }

    n = (Lib3dsMeshInstanceNode*)node;
    if (instance_name) {
        strcpy(n->instance_name, instance_name);
    }

    lib3ds_track_resize(&n->pos_track, 1);
    if (pos0) {
        lib3ds_vector_copy(n->pos_track.keys[0].value, pos0);
    }

    lib3ds_track_resize(&n->scl_track, 1);
    if (scl0) {
        lib3ds_vector_copy(n->scl_track.keys[0].value, scl0);
    } else {
        lib3ds_vector_make(n->scl_track.keys[0].value, 1, 1, 1);
    }

    lib3ds_track_resize(&n->rot_track, 1);
    if (rot0) {
        for (i = 0; i < 4; ++i) n->rot_track.keys[0].value[i] = rot0[i];
    } else {
        for (i = 0; i < 4; ++i) n->rot_track.keys[0].value[i] = 0;
    }

    return n;
}


Lib3dsCameraNode* 
lib3ds_node_new_camera(Lib3dsCamera *camera) {
    Lib3dsNode *node = lib3ds_node_new(LIB3DS_NODE_CAMERA);
    Lib3dsCameraNode *n;
    
    assert(camera);
    node = lib3ds_node_new(LIB3DS_NODE_CAMERA);
    strcpy(node->name, camera->name);

    n = (Lib3dsCameraNode*)node;
    lib3ds_track_resize(&n->pos_track, 1);
    lib3ds_vector_copy(n->pos_track.keys[0].value, camera->position);

    lib3ds_track_resize(&n->fov_track, 1);
    n->fov_track.keys[0].value[0] = camera->fov;
<commit_msg>Fixed Coverity reported issue.  This fix addresses a memory leak.<commit_after>/*
    Copyright (C) 1996-2008 by <NAME> <www.kyprianidis.com>
    All rights reserved.
    
    This program is free  software: you can redistribute it and/or modify 
    it under the terms of the GNU Lesser General Public License as published 
    by the Free Software Foundation, either version 2.1 of the License, or 
    (at your option) any later version.

    Thisprogram  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 Lesser General Public License for more details.
    
    You should  have received a copy of the GNU Lesser General Public License
    along with  this program; If not, see <http://www.gnu.org/licenses/>. 
*/
#include "lib3ds_impl.h"


/*!
 * Create and return a new node object.
 *
 * The node is returned with an identity matrix. All other fields
 * are zero.
 *
 * \return Lib3dsNode
 */
Lib3dsNode*
lib3ds_node_new(Lib3dsNodeType type) {
    Lib3dsNode *node;
    switch (type) {
        case LIB3DS_NODE_AMBIENT_COLOR: {
            Lib3dsAmbientColorNode *n = (Lib3dsAmbientColorNode*)calloc(sizeof(Lib3dsAmbientColorNode), 1);
            node = (Lib3dsNode*)n;
            strcpy(node->name, "$AMBIENT$");
            n->color_track.type = LIB3DS_TRACK_VECTOR;
            break;
        }

        case LIB3DS_NODE_MESH_INSTANCE: {
            Lib3dsMeshInstanceNode *n = (Lib3dsMeshInstanceNode*)calloc(sizeof(Lib3dsMeshInstanceNode), 1);
            node = (Lib3dsNode*)n;
            strcpy(node->name, "$$$DUMMY");
            n->pos_track.type = LIB3DS_TRACK_VECTOR;
            n->scl_track.type = LIB3DS_TRACK_VECTOR;
            n->rot_track.type = LIB3DS_TRACK_QUAT;
            n->hide_track.type = LIB3DS_TRACK_BOOL;
            break;
        }

        case LIB3DS_NODE_CAMERA: {
            Lib3dsCameraNode *n = (Lib3dsCameraNode*)calloc(sizeof(Lib3dsCameraNode), 1);
            node = (Lib3dsNode*)n;
            n->pos_track.type = LIB3DS_TRACK_VECTOR;
            n->fov_track.type = LIB3DS_TRACK_FLOAT;
            n->roll_track.type = LIB3DS_TRACK_FLOAT;
            break;
        }

        case LIB3DS_NODE_CAMERA_TARGET: {
            Lib3dsTargetNode *n = (Lib3dsTargetNode*)calloc(sizeof(Lib3dsTargetNode), 1);
            node = (Lib3dsNode*)n;
            n->pos_track.type = LIB3DS_TRACK_VECTOR;
            break;
        }

        case LIB3DS_NODE_OMNILIGHT: {
            Lib3dsOmnilightNode *n = (Lib3dsOmnilightNode*)calloc(sizeof(Lib3dsOmnilightNode), 1);
            node = (Lib3dsNode*)n;
            n->pos_track.type = LIB3DS_TRACK_VECTOR;
            n->color_track.type = LIB3DS_TRACK_VECTOR;
            break;
        }

        case LIB3DS_NODE_SPOTLIGHT: {
            Lib3dsSpotlightNode *n = (Lib3dsSpotlightNode*)calloc(sizeof(Lib3dsSpotlightNode), 1);
            node = (Lib3dsNode*)n;
            n->pos_track.type = LIB3DS_TRACK_VECTOR;
            n->color_track.type = LIB3DS_TRACK_VECTOR;
            n->hotspot_track.type = LIB3DS_TRACK_FLOAT;
            n->falloff_track.type = LIB3DS_TRACK_FLOAT;
            n->roll_track.type = LIB3DS_TRACK_FLOAT;
            break;
        }

        case LIB3DS_NODE_SPOTLIGHT_TARGET: {
            Lib3dsTargetNode *n = (Lib3dsTargetNode*)calloc(sizeof(Lib3dsTargetNode), 1);
            node = (Lib3dsNode*)n;
            n->pos_track.type = LIB3DS_TRACK_VECTOR;
            break;
        }

        default:
            assert(0);
            return NULL;
    }

    node->type = type;
    node->node_id = 65535;
    node->user_id = 65535;
    lib3ds_matrix_identity(node->matrix);
    return node;
}


Lib3dsAmbientColorNode* 
lib3ds_node_new_ambient_color(float color0[3]) {
    Lib3dsNode *node;
    Lib3dsAmbientColorNode *n;

    node = lib3ds_node_new(LIB3DS_NODE_AMBIENT_COLOR);

    n = (Lib3dsAmbientColorNode*)node;
    lib3ds_track_resize(&n->color_track, 1);
    if (color0) {
        lib3ds_vector_copy(n->color_track.keys[0].value, color0);
    } else {
        lib3ds_vector_zero(n->color_track.keys[0].value);
    }

    return n;
}


Lib3dsMeshInstanceNode* 
lib3ds_node_new_mesh_instance(Lib3dsMesh *mesh, const char *instance_name, float pos0[3], float scl0[3], float rot0[4]) {
    Lib3dsNode *node;
    Lib3dsMeshInstanceNode *n;
    int i;

    node = lib3ds_node_new(LIB3DS_NODE_MESH_INSTANCE);
    if (mesh) {
        strcpy(node->name, mesh->name);
    } else {
        strcpy(node->name, "$$$DUMMY");
    }

    n = (Lib3dsMeshInstanceNode*)node;
    if (instance_name) {
        strcpy(n->instance_name, instance_name);
    }

    lib3ds_track_resize(&n->pos_track, 1);
    if (pos0) {
        lib3ds_vector_copy(n->pos_track.keys[0].value, pos0);
    }

    lib3ds_track_resize(&n->scl_track, 1);
    if (scl0) {
        lib3ds_vector_copy(n->scl_track.keys[0].value, scl0);
    } else {
        lib3ds_vector_make(n->scl_track.keys[0].value, 1, 1, 1);
    }

    lib3ds_track_resize(&n->rot_track, 1);
    if (rot0) {
        for (i = 0; i < 4; ++i) n->rot_track.keys[0].value[i] = rot0[i];
    } else {
        for (i = 0; i < 4; ++i) n->rot_track.keys[0].value[i] = 0;
    }

    return n;
}


Lib3dsCameraNode* 
lib3ds_node_new_camera(Lib3dsCamera *camera) {
    Lib3dsNode *node;
    Lib3dsCameraNode *n;
    
    assert(camera);
    node = lib3ds_node_new(LIB3DS_NODE_CAMERA);
    strcpy(node->name, camera->name);

    n = (Lib3dsCameraNode*)node;
    lib3ds_track_resize(&n->pos_track, 1);
    lib3ds_vector_copy(n->pos_track.keys[0].value, camera->position);

    lib3ds_track_resize(&n->fov_track, 1);
    n->fov_track.keys[0].value[0] = camera->fov;

{'fraction_non_alphanumeric': 0.11073416840640223, 'fraction_numerical': 0.02835768963117606, 'mean_word_length': 2.598435054773083, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4725996', 'n_tokens_mistral': 4263, 'n_tokens_neox': 4086, 'n_words': 754}