Files
test/source/blender/blenkernel/intern/multires_reshape.cc
Sergey Sharybin a12a8a71bb Remove "All Rights Reserved" from Blender Foundation copyright code
The goal is to solve confusion of the "All rights reserved" for licensing
code under an open-source license.

The phrase "All rights reserved" comes from a historical convention that
required this phrase for the copyright protection to apply. This convention
is no longer relevant.

However, even though the phrase has no meaning in establishing the copyright
it has not lost meaning in terms of licensing.

This change makes it so code under the Blender Foundation copyright does
not use "all rights reserved". This is also how the GPL license itself
states how to apply it to the source code:

    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    This program is free software ...

This change does not change copyright notice in cases when the copyright
is dual (BF and an author), or just an author of the code. It also does
mot change copyright which is inherited from NaN Holding BV as it needs
some further investigation about what is the proper way to handle it.
2023-03-30 10:51:59 +02:00

282 lines
10 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2020 Blender Foundation */
/** \file
* \ingroup bke
*/
#include "MEM_guardedalloc.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_modifier_types.h"
#include "DNA_scene_types.h"
#include "BKE_customdata.h"
#include "BKE_lib_id.h"
#include "BKE_mesh.hh"
#include "BKE_mesh_runtime.h"
#include "BKE_modifier.h"
#include "BKE_multires.h"
#include "BKE_subdiv.h"
#include "BKE_subsurf.h"
#include "BLI_math_vector.h"
#include "DEG_depsgraph_query.h"
#include "multires_reshape.hh"
/* -------------------------------------------------------------------- */
/** \name Reshape from object
* \{ */
bool multiresModifier_reshapeFromVertcos(Depsgraph *depsgraph,
Object *object,
MultiresModifierData *mmd,
const float (*vert_coords)[3],
const int num_vert_coords)
{
MultiresReshapeContext reshape_context;
if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
return false;
}
multires_reshape_store_original_grids(&reshape_context);
multires_reshape_ensure_grids(static_cast<Mesh *>(object->data), reshape_context.top.level);
if (!multires_reshape_assign_final_coords_from_vertcos(
&reshape_context, vert_coords, num_vert_coords)) {
multires_reshape_context_free(&reshape_context);
return false;
}
multires_reshape_smooth_object_grids_with_details(&reshape_context);
multires_reshape_object_grids_to_tangent_displacement(&reshape_context);
multires_reshape_context_free(&reshape_context);
return true;
}
bool multiresModifier_reshapeFromObject(Depsgraph *depsgraph,
MultiresModifierData *mmd,
Object *dst,
Object *src)
{
Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
Object *src_eval = DEG_get_evaluated_object(depsgraph, src);
Mesh *src_mesh_eval = mesh_get_eval_final(depsgraph, scene_eval, src_eval, &CD_MASK_BAREMESH);
int num_deformed_verts;
float(*deformed_verts)[3] = BKE_mesh_vert_coords_alloc(src_mesh_eval, &num_deformed_verts);
const bool result = multiresModifier_reshapeFromVertcos(
depsgraph, dst, mmd, deformed_verts, num_deformed_verts);
MEM_freeN(deformed_verts);
return result;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Reshape from modifier
* \{ */
bool multiresModifier_reshapeFromDeformModifier(Depsgraph *depsgraph,
Object *object,
MultiresModifierData *mmd,
ModifierData *deform_md)
{
MultiresModifierData highest_mmd = blender::dna::shallow_copy(*mmd);
highest_mmd.sculptlvl = highest_mmd.totlvl;
highest_mmd.lvl = highest_mmd.totlvl;
highest_mmd.renderlvl = highest_mmd.totlvl;
/* Create mesh for the multires, ignoring any further modifiers (leading
* deformation modifiers will be applied though). */
Mesh *multires_mesh = BKE_multires_create_mesh(depsgraph, object, &highest_mmd);
int num_deformed_verts;
float(*deformed_verts)[3] = BKE_mesh_vert_coords_alloc(multires_mesh, &num_deformed_verts);
/* Apply deformation modifier on the multires, */
ModifierEvalContext modifier_ctx{};
modifier_ctx.depsgraph = depsgraph;
modifier_ctx.object = object;
modifier_ctx.flag = MOD_APPLY_USECACHE | MOD_APPLY_IGNORE_SIMPLIFY;
BKE_modifier_deform_verts(
deform_md, &modifier_ctx, multires_mesh, deformed_verts, multires_mesh->totvert);
BKE_id_free(nullptr, multires_mesh);
/* Reshaping */
bool result = multiresModifier_reshapeFromVertcos(
depsgraph, object, &highest_mmd, deformed_verts, num_deformed_verts);
/* Cleanup */
MEM_freeN(deformed_verts);
return result;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Reshape from grids
* \{ */
bool multiresModifier_reshapeFromCCG(const int tot_level, Mesh *coarse_mesh, SubdivCCG *subdiv_ccg)
{
MultiresReshapeContext reshape_context;
if (!multires_reshape_context_create_from_ccg(
&reshape_context, subdiv_ccg, coarse_mesh, tot_level)) {
return false;
}
multires_ensure_external_read(coarse_mesh, reshape_context.top.level);
multires_reshape_store_original_grids(&reshape_context);
multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
if (!multires_reshape_assign_final_coords_from_ccg(&reshape_context, subdiv_ccg)) {
multires_reshape_context_free(&reshape_context);
return false;
}
multires_reshape_smooth_object_grids_with_details(&reshape_context);
multires_reshape_object_grids_to_tangent_displacement(&reshape_context);
multires_reshape_context_free(&reshape_context);
return true;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Subdivision
* \{ */
void multiresModifier_subdivide(Object *object,
MultiresModifierData *mmd,
const eMultiresSubdivideModeType mode)
{
const int top_level = mmd->totlvl + 1;
multiresModifier_subdivide_to_level(object, mmd, top_level, mode);
}
void multiresModifier_subdivide_to_level(Object *object,
MultiresModifierData *mmd,
const int top_level,
const eMultiresSubdivideModeType mode)
{
if (top_level <= mmd->totlvl) {
return;
}
Mesh *coarse_mesh = static_cast<Mesh *>(object->data);
if (coarse_mesh->totloop == 0) {
/* If there are no loops in the mesh implies there is no CD_MDISPS as well. So can early output
* from here as there is nothing to subdivide. */
return;
}
MultiresReshapeContext reshape_context;
/* There was no multires at all, all displacement is at 0. Can simply make sure all mdisps grids
* are allocated at a proper level and return. */
const bool has_mdisps = CustomData_has_layer(&coarse_mesh->ldata, CD_MDISPS);
if (!has_mdisps) {
CustomData_add_layer(&coarse_mesh->ldata, CD_MDISPS, CD_SET_DEFAULT, coarse_mesh->totloop);
}
/* NOTE: Subdivision happens from the top level of the existing multires modifier. If it is set
* to 0 and there is mdisps layer it would mean that the modifier went out of sync with the data.
* This happens when, for example, linking modifiers from one object to another.
*
* In such cases simply ensure grids to be the proper level.
*
* If something smarter is needed it is up to the operators which does data synchronization, so
* that the mdisps layer is also synchronized. */
if (!has_mdisps || top_level == 1 || mmd->totlvl == 0) {
multires_reshape_ensure_grids(coarse_mesh, top_level);
if (ELEM(mode, MULTIRES_SUBDIVIDE_LINEAR, MULTIRES_SUBDIVIDE_SIMPLE)) {
multires_subdivide_create_tangent_displacement_linear_grids(object, mmd);
}
else {
multires_set_tot_level(object, mmd, top_level);
}
return;
}
multires_flush_sculpt_updates(object);
if (!multires_reshape_context_create_from_modifier(&reshape_context, object, mmd, top_level)) {
return;
}
multires_reshape_store_original_grids(&reshape_context);
multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
multires_reshape_assign_final_elements_from_orig_mdisps(&reshape_context);
/* Free original grids which makes it so smoothing with details thinks all the details were
* added against base mesh's limit surface. This is similar behavior to as if we've done all
* displacement in sculpt mode at the old top level and then propagated to the new top level. */
multires_reshape_free_original_grids(&reshape_context);
if (ELEM(mode, MULTIRES_SUBDIVIDE_LINEAR, MULTIRES_SUBDIVIDE_SIMPLE)) {
multires_reshape_smooth_object_grids(&reshape_context, mode);
}
else {
multires_reshape_smooth_object_grids_with_details(&reshape_context);
}
multires_reshape_object_grids_to_tangent_displacement(&reshape_context);
multires_reshape_context_free(&reshape_context);
multires_set_tot_level(object, mmd, top_level);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Apply base
* \{ */
void multiresModifier_base_apply(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
{
multires_force_sculpt_rebuild(object);
MultiresReshapeContext reshape_context;
if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
return;
}
multires_reshape_store_original_grids(&reshape_context);
/* At this point base_mesh is object's mesh, the subdiv is initialized to the deformed state of
* the base mesh.
* Store coordinates of top level grids in object space which will define true shape we would
* want to reshape to after modifying the base mesh. */
multires_reshape_assign_final_coords_from_mdisps(&reshape_context);
/* For modifying base mesh we only want to consider deformation caused by multires displacement
* and ignore all deformation which might be caused by deformation modifiers leading the multires
* one.
* So refine the subdiv to the original mesh vertices positions, which will also need to make
* it so object space displacement is re-evaluated for them (as in, can not re-use any knowledge
* from the final coordinates in the object space ). */
multires_reshape_apply_base_refine_from_base(&reshape_context);
/* Modify original mesh coordinates. This happens in two steps:
* - Coordinates are set to their final location, where they are intended to be in the final
* result.
* - Heuristic moves them a bit, kind of canceling out the effect of subsurf (so then when
* multires modifier applies subsurf vertices are placed at the desired location). */
multires_reshape_apply_base_update_mesh_coords(&reshape_context);
multires_reshape_apply_base_refit_base_mesh(&reshape_context);
/* Reshape to the stored final state.
* Not that the base changed, so the subdiv is to be refined to the new positions. Unfortunately,
* this can not be done foe entirely cheap: if there were deformation modifiers prior to the
* multires they need to be re-evaluated for the new base mesh. */
multires_reshape_apply_base_refine_from_deform(&reshape_context);
multires_reshape_object_grids_to_tangent_displacement(&reshape_context);
multires_reshape_context_free(&reshape_context);
}
/** \} */