/*
 *******************************************************************************
 *
 *  Copyright 2026 RIEGL Laser Measurement Systems
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *
 *******************************************************************************
 */
/*!
 *******************************************************************************
 *
 * \file    queryUpdate.h
 * \author  RIEGL LMS GmbH, Austria
 * \brief   Point update query
 * \version 2015-10-14/AW: Initial version
 *
 * This class can be used to update (modify) attributes of existing points.
 *
 * \see riegl::rdb::Pointcloud::update()
 *
 *******************************************************************************
 */

#ifndef RIEGL_RDB_POINTCLOUD_QUERYUPDATE_H
#define RIEGL_RDB_POINTCLOUD_QUERYUPDATE_H

//---< INCLUDES >---------------------------------------------------------------

#include "riegl/rdb.h"

//---< TYPE DEFINITIONS >-------------------------------------------------------

typedef struct RDBPointcloudQueryUpdate RDBPointcloudQueryUpdate; // forward declaration of implementation details

//---< FUNCTIONS >--------------------------------------------------------------

RDB_LIBRARY_API_BEGIN

//______________________________________________________________________________
/*!
 * \brief Constructor
 * \see   riegl::rdb::Pointcloud::update()
 */
RDB_FUNCTION(rdb_pointcloud_query_update_new,
    RDBContext                *context,    //!< [in] library context
    RDBPointcloud             *pointcloud, //!< [in] point cloud
    RDBPointcloudQueryUpdate **query       //!< [out] query handle
);

//______________________________________________________________________________
/*!
 * \brief Destroy query instance
 */
RDB_FUNCTION(rdb_pointcloud_query_update_delete,
    RDBContext                *context, //!< [in] library context
    RDBPointcloudQueryUpdate **query    //!< [in] query handle
);

//______________________________________________________________________________
/*!
 * \brief Bind attribute buffer
 *
 * Use this function to define a source buffer for a point attribute.
 * Exactly one buffer can be defined for an attribute (i.e. only the
 * most recently defined buffer will be used).
 *
 * You can but don't need to define a buffer for each attribute. If
 * no buffer is defined for an attribute, it remains unchanged.
 *
 * The buffer is expected to be n*s*d bytes large, where
 * __n__ is the number of points defined in next(),
 * __s__ is the size of one element as defined by 'dataType' and
 * __d__ is the number of attribute dimensions (elements).
 *
 * \note This function just stores the buffer pointer - it does
 *       __NOT__ copy the data contained in the buffer. So make
 *       sure that the buffer remains valid until you call next().
 *
 * \note This function expects a buffer for the point ID attribute.
 *
 * \see riegl::rdb::pointcloud::PointAttributes
 */
RDB_FUNCTION(rdb_pointcloud_query_update_bind,
    RDBContext               *context,                    //!< [in] library context
    RDBPointcloudQueryUpdate *query,                      //!< [in] query handle
    RDBString                 attribute,                  //!< [in] attribute name
    uint32_t                  dataType,                   //!< [in] buffer data type \see dataTypes.h
    const void               *buffer,                     //!< [in] buffer location
    int32_t                   stride RDB_DEFAULT_VALUE(0) //!< [in] bytes between beginnings of successive elements (0: auto)
);

//______________________________________________________________________________
/*!
 * \brief Update points
 *
 * Use this function to actually read the point attributes from
 * all defined buffers and update the points in the database.
 *
 * Afterwards you may re-fill the buffers or define new buffers
 * with bind() and call next() again until all points have been
 * updated.
 *
 * \remark It is assumed that the points are given in the exact same order as
 *         returned by the select query (riegl::rdb::pointcloud::QuerySelect).
 *         If the points are given in a different order, the update will still
 *         work but may take more time to finish. Of course it is not required
 *         to update all points (you don't need to provide points that you do
 *         not want to update).
 *
 * \note IEEE-754 "NaN" values contained in floating point source
 *       buffers are ignored and the attribute's default value is
 *       used instead. Furthermore IEEE-754 "Infinity" values will
 *       always cause next() to fail with error code 10414, i.e.
 *       riegl::rdb::Error::QueryAttributeValueOutOfRange.
 *
 * \note Since version 2.4.2, unit vector point attributes (e.g.
 *       "riegl.direction") are automatically normalized (divided
 *       by the vector length) on insertion. This is done for all
 *       point attributes that meet the following requirements:
 *       `length` is 3, `minimumValue` is -1, `maximumValue` is 1,
 *       `resolution` is not 1, `scaleFactor` is 1, `unitSymbol` is
 *       empty, `invalidValue` is undefined and `tags` contains the
 *       "direction" tag but not the "do-not-normalize" tag.
 *
 * \warning If you want to modify the primary point attribute (usually
 *          the point coordinates), you __must__ either read __all__ points
 *          with a select query (class QuerySelect) first and update them
 *          later, or use two different instances of riegl::rdb::Pointcloud
 *          on the same database and use one instance to read (select) and
 *          the other instance to write (update) the points. In other words:
 *          It is not allowed to alternately call next() on an instance of
 *          QuerySelect and an instance of QueryUpdate if both were started
 *          on the same Pointcloud instance and the goal is to modify the
 *          primary point attribute.
 *
 * \returns the number of points updated
 */
RDB_FUNCTION(rdb_pointcloud_query_update_next,
    RDBContext               *context,                       //!< [in] library context
    RDBPointcloudQueryUpdate *query,                         //!< [in] query handle
    uint32_t                  count,                         //!< [in] size of source buffers in terms of points
    uint32_t                 *processed RDB_DEFAULT_VALUE(0) //!< [out] number of processed points (optional)
);

RDB_LIBRARY_API_END

#endif // RIEGL_RDB_POINTCLOUD_QUERYUPDATE_H
