<commit_before>#include <thread>
#include <vector>
#include <string>
#include <cassert>
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include <glm/glm.hpp>
#include <glm/ext.hpp>
#include <vtkSmartPointer.h>
#include <vtkXMLImageDataReader.h>
#include <vtkImageData.h>
#include <vtkThreshold.h>
#include <vtkContourForests.h>
#include <vtkPersistenceCurve.h>
#include <vtkPersistenceDiagram.h>
#include <vtkTopologicalSimplification.h>

#include "imgui-1.49/imgui.h"
#include "imgui-1.49/imgui_impl_sdl_gl3.h"
#include "glt/gl_core_4_5.h"
#include "glt/arcball_camera.h"
#include "glt/debug.h"
#include "glt/buffer_allocator.h"

#include "transfer_function.h"
#include "volume.h"
#include "tree_widget.h"
#include "persistence_curve_widget.h"

static size_t WIN_WIDTH = 1280;
static size_t WIN_HEIGHT = 720;
static unsigned int debuglevel = 0;

void run_app(SDL_Window *win, const std::vector<std::string> &args);
void setup_window(SDL_Window *&win, SDL_GLContext &ctx);
void DefaultCommands(int argc, const char **argv) {
    for (int i = 1; i < argc; ++i) {
	std::string str(argv[i]);
	if (str.compare("-debug") == 0) {
	    debuglevel = std::stoi(argv[++i]);
	}
    }
}
int main(int argc, const char **argv) {
        DefaultCommands(argc, argv);
	SDL_Window *win = nullptr;
	SDL_GLContext ctx = nullptr;
	setup_window(win, ctx);
	run_app(win, std::vector<std::string>(argv, argv + argc));
	ImGui_ImplSdlGL3_Shutdown();
	SDL_GL_DeleteContext(ctx);
	SDL_DestroyWindow(win);
	SDL_Quit();
	return 0;
}
void run_app(SDL_Window *win, const std::vector<std::string> &args) {
	// Read the volume data using vtk
	vtkSmartPointer<vtkXMLImageDataReader> reader
		= vtkSmartPointer<vtkXMLImageDataReader>::New();
	reader->SetFileName(args[1].c_str());
	reader->Update();
	vtkImageData *vol = reader->GetOutput();
	assert(vol);

	glm::vec3 vol_render_size;
	for (size_t i = 0; i < 3; ++i) {
		vol_render_size[i] = vol->GetSpacing()[i] * vol->GetDimensions()[i];
	}
	std::shared_ptr<glt::BufferAllocator> allocator = std::make_shared<glt::BufferAllocator>(size_t(64e6));

	glm::mat4 proj_mat = glm::perspective(glm::radians(65.f),
			static_cast<float>(WIN_WIDTH) / WIN_HEIGHT, 0.1f, 2000.f);
	glt::ArcBallCamera camera(glm::lookAt(glm::vec3{0.0, 0.0, vol_render_size.z},
			glm::vec3{0.0, 0.0, 0}, glm::vec3{0, 1, 0}),
			2.0, 75.0, {WIN_WIDTH, WIN_HEIGHT});

	// Note the vec3 is padded to a vec4 size, so we need a bit more room
	auto viewing_buf = allocator->alloc(2 * sizeof(glm::mat4) + sizeof(glm::vec4), glt::BufAlignment::UNIFORM_BUFFER);
	glBindBuffer(GL_UNIFORM_BUFFER, viewing_buf.buffer);
	glBindBufferRange(GL_UNIFORM_BUFFER, 0, viewing_buf.buffer, viewing_buf.offset, viewing_buf.size);
	{
		char *buf = static_cast<char*>(viewing_buf.map(GL_UNIFORM_BUFFER,
					GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_WRITE_BIT));
		glm::mat4 *mats = reinterpret_cast<glm::mat4*>(buf);
		// While it says vec3 in the buffer with std140 layout they'll be padded to vec4's
		glm::vec4 *vecs = reinterpret_cast<glm::vec4*>(buf + 2 * sizeof(glm::mat4));
		mats[0] = proj_mat;
		mats[1] = camera.transform();
		vecs[0] = glm::vec4{camera.eye_pos(), 1};

		glBindBufferRange(GL_UNIFORM_BUFFER, 0, viewing_buf.buffer, viewing_buf.offset, viewing_buf.size);
		viewing_buf.unmap(GL_UNIFORM_BUFFER);
	}

	// TODO: Contour/Split/Merge tree widget, pick a branch or multiple branches
	// in this widget and select those segments in the volume rendering.
	// The volume rendering picking can be done by looking up the same voxel position
	// in a separate Segmentation ID texture and discarding voxels accordingly.
	// The tree widget can be done by translating the arcs/points from TTK to a
	// more abstract representation, take the min/max ImageFile value for each group
	// of points for a segmentation id and draw the graph with these points connecting to
	// each other and splitting. The points come in the same order as the arcs,
	// where arc 0 is segmentation id 0 and starts at point 0 and ends at point 1.
	// Can also check the point locations for the node/arc points to confirm this.
	PersistenceCurveWidget persistence_curve_widget(reader, debuglevel); // come on ... let me debug !

	vtkSmartPointer<vtkContourForests> contour_forest
		= vtkSmartPointer<vtkContourForests>::New();<commit_msg>remove some old comments<commit_after>#include <thread>
#include <vector>
#include <string>
#include <cassert>
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include <glm/glm.hpp>
#include <glm/ext.hpp>
#include <vtkSmartPointer.h>
#include <vtkXMLImageDataReader.h>
#include <vtkImageData.h>
#include <vtkThreshold.h>
#include <vtkContourForests.h>
#include <vtkPersistenceCurve.h>
#include <vtkPersistenceDiagram.h>
#include <vtkTopologicalSimplification.h>

#include "imgui-1.49/imgui.h"
#include "imgui-1.49/imgui_impl_sdl_gl3.h"
#include "glt/gl_core_4_5.h"
#include "glt/arcball_camera.h"
#include "glt/debug.h"
#include "glt/buffer_allocator.h"

#include "transfer_function.h"
#include "volume.h"
#include "tree_widget.h"
#include "persistence_curve_widget.h"

static size_t WIN_WIDTH = 1280;
static size_t WIN_HEIGHT = 720;
static unsigned int debuglevel = 0;

void run_app(SDL_Window *win, const std::vector<std::string> &args);
void setup_window(SDL_Window *&win, SDL_GLContext &ctx);
void DefaultCommands(int argc, const char **argv) {
    for (int i = 1; i < argc; ++i) {
	std::string str(argv[i]);
	if (str.compare("-debug") == 0) {
	    debuglevel = std::stoi(argv[++i]);
	}
    }
}
int main(int argc, const char **argv) {
        DefaultCommands(argc, argv);
	SDL_Window *win = nullptr;
	SDL_GLContext ctx = nullptr;
	setup_window(win, ctx);
	run_app(win, std::vector<std::string>(argv, argv + argc));
	ImGui_ImplSdlGL3_Shutdown();
	SDL_GL_DeleteContext(ctx);
	SDL_DestroyWindow(win);
	SDL_Quit();
	return 0;
}
void run_app(SDL_Window *win, const std::vector<std::string> &args) {
	// Read the volume data using vtk
	vtkSmartPointer<vtkXMLImageDataReader> reader
		= vtkSmartPointer<vtkXMLImageDataReader>::New();
	reader->SetFileName(args[1].c_str());
	reader->Update();
	vtkImageData *vol = reader->GetOutput();
	assert(vol);

	glm::vec3 vol_render_size;
	for (size_t i = 0; i < 3; ++i) {
		vol_render_size[i] = vol->GetSpacing()[i] * vol->GetDimensions()[i];
	}
	std::shared_ptr<glt::BufferAllocator> allocator = std::make_shared<glt::BufferAllocator>(size_t(64e6));

	glm::mat4 proj_mat = glm::perspective(glm::radians(65.f),
			static_cast<float>(WIN_WIDTH) / WIN_HEIGHT, 0.1f, 2000.f);
	glt::ArcBallCamera camera(glm::lookAt(glm::vec3{0.0, 0.0, vol_render_size.z},
			glm::vec3{0.0, 0.0, 0}, glm::vec3{0, 1, 0}),
			2.0, 75.0, {WIN_WIDTH, WIN_HEIGHT});

	// Note the vec3 is padded to a vec4 size, so we need a bit more room
	auto viewing_buf = allocator->alloc(2 * sizeof(glm::mat4) + sizeof(glm::vec4), glt::BufAlignment::UNIFORM_BUFFER);
	glBindBuffer(GL_UNIFORM_BUFFER, viewing_buf.buffer);
	glBindBufferRange(GL_UNIFORM_BUFFER, 0, viewing_buf.buffer, viewing_buf.offset, viewing_buf.size);
	{
		char *buf = static_cast<char*>(viewing_buf.map(GL_UNIFORM_BUFFER,
					GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_WRITE_BIT));
		glm::mat4 *mats = reinterpret_cast<glm::mat4*>(buf);
		// While it says vec3 in the buffer with std140 layout they'll be padded to vec4's
		glm::vec4 *vecs = reinterpret_cast<glm::vec4*>(buf + 2 * sizeof(glm::mat4));
		mats[0] = proj_mat;
		mats[1] = camera.transform();
		vecs[0] = glm::vec4{camera.eye_pos(), 1};

		glBindBufferRange(GL_UNIFORM_BUFFER, 0, viewing_buf.buffer, viewing_buf.offset, viewing_buf.size);
		viewing_buf.unmap(GL_UNIFORM_BUFFER);
	}

	PersistenceCurveWidget persistence_curve_widget(reader, debuglevel);

	vtkSmartPointer<vtkContourForests> contour_forest
		= vtkSmartPointer<vtkContourForests>::New();
{'fraction_non_alphanumeric': 0.142230252968508, 'fraction_numerical': 0.021166752710376872, 'mean_word_length': 5.785464098073555, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1317140', 'n_tokens_mistral': 3082, 'n_tokens_neox': 2905, 'n_words': 646}