refactoredCPPNeuronMesher
|
#include <GLFW/glfw3.h>
#include <GL/glu.h>
#include "tinyfiledialogs.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <array>
#include <tuple>
Classes | |
struct | SWCNode |
Structure representing a single node in an SWC neuron morphology. More... | |
Functions | |
void | printHelpText () |
Prints keyboard and mouse controls to the console. More... | |
std::vector< SWCNode > | loadSWC (const std::string &filename) |
Loads neuron data from an SWC file. More... | |
void | computeBounds (const std::vector< SWCNode > &nodes, float &minX, float &maxX, float &minY, float &maxY, float &minZ, float &maxZ, float ¢erX, float ¢erY, float ¢erZ, float &radius) |
void | openNeuronFile (std::vector< SWCNode > &nodes, float &minX, float &maxX, float &minY, float &maxY, float &minZ, float &maxZ, float ¢erX, float ¢erY, float ¢erZ, float &radius) |
void | setColorByType (int type) |
Sets the OpenGL color based on SWC node type. More... | |
void | drawSimpleSphere (const SWCNode &node, float size=0.5f) |
void | drawSimpleLine (const SWCNode &a, const SWCNode &b) |
Draws a simple line between two nodes. More... | |
void | drawSphere (const SWCNode &node, int slices=6, int stacks=6) |
Draws a sphere with specified geometric detail. More... | |
void | drawCylinder (const SWCNode &a, const SWCNode &b, int segments=6) |
Draws a cylinder connecting two nodes. More... | |
void | drawBoundingBox (float minX, float maxX, float minY, float maxY, float minZ, float maxZ) |
void | renderSWC (const std::vector< SWCNode > &nodes) |
Renders a complete SWC neuron morphology. More... | |
void | keyCallback (GLFWwindow *window, int key, int scancode, int action, int mods) |
Handles keyboard input events. More... | |
void | mouseButtonCallback (GLFWwindow *window, int button, int action, int mods) |
Handles mouse button input events. More... | |
void | cursorPosCallback (GLFWwindow *window, double xpos, double ypos) |
Handles mouse cursor movement. More... | |
void | scrollCallback (GLFWwindow *window, double xoffset, double yoffset) |
Handles mouse scroll events. More... | |
void | setupLighting () |
void | setupCamera (float cx, float cy, float cz, float radius, int width, int height) |
int | main (int argc, char **argv) |
Variables | |
int | renderMode = 1 |
Current rendering mode (1-5) More... | |
float | rotateX = 0.0f |
Camera rotation around X and Y axes (in degrees) More... | |
float | rotateY = 0.0f |
float | zoom = 1.0f |
Camera zoom factor (higher values = more zoomed in) More... | |
float | panX = 0.0f |
Camera pan offset in X and Y directions. More... | |
float | panY = 0.0f |
bool | dragging = false |
Mouse drag state flags. More... | |
bool | rightDragging = false |
double | lastX |
Last recorded mouse cursor position. More... | |
double | lastY |
void computeBounds | ( | const std::vector< SWCNode > & | nodes, |
float & | minX, | ||
float & | maxX, | ||
float & | minY, | ||
float & | maxY, | ||
float & | minZ, | ||
float & | maxZ, | ||
float & | centerX, | ||
float & | centerY, | ||
float & | centerZ, | ||
float & | radius | ||
) |
void cursorPosCallback | ( | GLFWwindow * | window, |
double | xpos, | ||
double | ypos | ||
) |
Handles mouse cursor movement.
Updates camera orientation or position based on mouse movement when:
window | The GLFW window that received the event |
xpos | The new cursor x-coordinate, relative to the left edge of the content area |
ypos | The new cursor y-coordinate, relative to the top edge of the content area |
Handles mouse cursor movement.
[in] | window | The GLFW window that received the event |
[in] | xpos | The new x-coordinate of the cursor, in screen coordinates |
[in] | ypos | The new y-coordinate of the cursor, in screen coordinates |
This callback function processes mouse movement to implement interactive 3D view manipulation. It provides the following functionality:
The function uses the difference between the current and last cursor positions to calculate the amount of rotation or panning to apply. The actual view transformation is applied in the main rendering loop using the global state variables modified by this function.
void drawBoundingBox | ( | float | minX, |
float | maxX, | ||
float | minY, | ||
float | maxY, | ||
float | minZ, | ||
float | maxZ | ||
) |
Draws a cylinder connecting two nodes.
a | First node (start of cylinder) |
b | Second node (end of cylinder) |
segments | Number of radial segments for the cylinder [default: 6] |
Renders a tapered cylinder that connects two nodes, with the radius at each end determined by the nodes' radii. The cylinder is colored according to the first node's type.
Draws a cylinder connecting two nodes.
[in] | a | The starting node of the cylinder |
[in] | b | The ending node of the cylinder |
[in] | segments | The number of sides around the cylinder |
This function draws a tapered cylinder connecting two nodes in 3D space. The cylinder's radius at each end is determined by the radius property of the corresponding node, allowing for smooth tapering of neurites.
The function handles the necessary coordinate system transformations to orient the cylinder correctly between the two points in 3D space. It uses OpenGL's matrix stack to ensure the transformations don't affect subsequent rendering operations.
Draws a simple line between two nodes.
a | First node (start point) |
b | Second node (end point) |
Renders a line in 3D space between two nodes using immediate mode OpenGL. The line width is determined by the nodes' radii.
Draws a simple line between two nodes.
[in] | a | The starting node of the line |
[in] | b | The ending node of the line |
This function renders a simple line segment between two nodes in 3D space using OpenGL's immediate mode line rendering. The line is drawn with the current OpenGL color and line width settings.
The function is typically used for wireframe representations of neuron morphologies or for debug visualization.
void drawSimpleSphere | ( | const SWCNode & | node, |
float | size = 0.5f |
||
) |
void drawSphere | ( | const SWCNode & | node, |
int | slices = 6 , |
||
int | stacks = 6 |
||
) |
Draws a sphere with specified geometric detail.
node | The SWC node to draw |
slices | Number of subdivisions around the z-axis (longitude) [default: 6] |
stacks | Number of subdivisions along the z-axis (latitude) [default: 6] |
Uses GLU quadric objects to render a sphere with the specified level of detail. The sphere is colored according to the node's type.
Draws a sphere with specified geometric detail.
[in] | node | The SWCNode containing the position and radius for the sphere |
[in] | slices | The number of subdivisions around the Z axis (longitude) |
[in] | stacks | The number of subdivisions along the Z axis (latitude) |
This function draws a sphere using OpenGL's GLU quadric primitives with smooth normal vectors for proper lighting calculations. The sphere's radius is taken from the node's radius property, and its position is set to the node's coordinates.
The function handles the OpenGL matrix stack appropriately and uses the current OpenGL color for rendering. The resolution of the sphere can be controlled through the slices and stacks parameters.
void keyCallback | ( | GLFWwindow * | window, |
int | key, | ||
int | scancode, | ||
int | action, | ||
int | mods | ||
) |
Handles keyboard input events.
Processes key presses and releases to control the visualization:
window | The GLFW window that received the event |
key | The keyboard key that was pressed or released |
scancode | The system-specific scancode of the key |
action | GLFW_PRESS, GLFW_RELEASE or GLFW_REPEAT |
mods | Bit field describing which modifier keys were held down |
Handles keyboard input events.
[in] | window | The GLFW window that received the event |
[in] | key | The keyboard key that was pressed or released |
[in] | scancode | The system-specific scancode of the key |
[in] | action | The key action (GLFW_PRESS, GLFW_RELEASE, or GLFW_REPEAT) |
[in] | mods | Bit field describing which modifier keys were held down |
This callback function processes keyboard input for controlling the neuron viewer. It implements the following keyboard shortcuts:
The function modifies global state for view parameters and triggers updates to the neuron visualization when geometry is refined or coarsened.
std::vector<SWCNode> loadSWC | ( | const std::string & | filename | ) |
Loads neuron data from an SWC file.
Parses an SWC (Standardized Waveform Container) file containing neuron morphology data and returns a vector of SWCNode objects.
filename | Path to the SWC file to load |
std::runtime_error | If the file cannot be opened or is malformed |
Loads neuron data from an SWC file.
[in] | filename | Path to the SWC or UGX file to load |
This function loads a neuron morphology from the specified file and returns its nodes as a vector of SWCNode objects. It supports both SWC and UGX file formats and performs preprocessing on the loaded data.
The function:
std::runtime_error | if the file cannot be read or parsed |
int main | ( | int | argc, |
char ** | argv | ||
) |
void mouseButtonCallback | ( | GLFWwindow * | window, |
int | button, | ||
int | action, | ||
int | mods | ||
) |
Handles mouse button input events.
Processes mouse button presses and releases for:
window | The GLFW window that received the event |
button | The mouse button that was pressed or released |
action | GLFW_PRESS or GLFW_RELEASE |
mods | Bit field describing which modifier keys were held down |
Handles mouse button input events.
[in] | window | The GLFW window that received the event |
[in] | button | The mouse button that was pressed or released |
[in] | action | The button action (GLFW_PRESS or GLFW_RELEASE) |
[in] | mods | Bit field describing which modifier keys were held down |
This callback function processes mouse button events to enable interactive 3D view manipulation. It tracks the following interactions:
The function updates global state variables (dragging, rightDragging) that are used by cursorPosCallback to implement the actual view transformations.
void openNeuronFile | ( | std::vector< SWCNode > & | nodes, |
float & | minX, | ||
float & | maxX, | ||
float & | minY, | ||
float & | maxY, | ||
float & | minZ, | ||
float & | maxZ, | ||
float & | centerX, | ||
float & | centerY, | ||
float & | centerZ, | ||
float & | radius | ||
) |
void printHelpText | ( | ) |
Prints keyboard and mouse controls to the console.
Displays a help message listing all available keyboard shortcuts and mouse controls for the neuron viewer application.
Prints keyboard and mouse controls to the console.
Displays the application's keyboard and mouse controls to the console. The help text is defined as a global string constant in globals.h.
void renderSWC | ( | const std::vector< SWCNode > & | nodes | ) |
Renders a complete SWC neuron morphology.
nodes | Vector of SWC nodes to render |
This is the main rendering function that draws an entire neuron morphology. It handles the display of all nodes and connections between them, with appropriate coloring based on node types. The function respects the current rendering mode and visualization settings from globals.h.
Renders a complete SWC neuron morphology.
[in] | nodes | A vector of SWCNode objects representing the neuron morphology |
This function renders a complete neuron morphology from SWC node data using different rendering modes controlled by the global renderMode
variable. The function handles both the rendering of nodes (soma, branch points, etc.) and the connections between them (dendrites, axons).
Supported render modes:
The function first renders all nodes according to the current render mode, then renders the connections between nodes. Node colors are determined by their SWC type using the setColorByType() function.
renderMode
variable to determine rendering style void scrollCallback | ( | GLFWwindow * | window, |
double | xoffset, | ||
double | yoffset | ||
) |
Handles mouse scroll events.
Zooms the view in/out based on vertical scroll input.
window | The GLFW window that received the event |
xoffset | The scroll offset along the x-axis (unused) |
yoffset | The scroll offset along the y-axis (positive = scroll up, negative = scroll down) |
Handles mouse scroll events.
[in] | window | The GLFW window that received the event |
[in] | xoffset | The scroll offset along the x-axis (unused in this implementation) |
[in] | yoffset | The scroll offset along the y-axis (positive for scroll up, negative for scroll down) |
This callback function processes mouse scroll wheel input to implement smooth zooming of the 3D view. It modifies the global zoom factor based on the vertical scroll amount.
The zoom factor is adjusted using an exponential scale (1.1^yoffset) to provide natural-feeling zoom behavior. The zoom level is clamped between 0.05 and 10.0 to prevent extreme zoom levels that might cause rendering issues or numerical instability.
void setColorByType | ( | int | type | ) |
Sets the OpenGL color based on SWC node type.
type | SWC node type identifier (1-7) |
Maps standard SWC node types to distinct colors for visualization. The color mapping follows the standard SWC color scheme:
[in] | type | The SWC node type identifier |
This function maps standard SWC node types to specific colors for visualization. The color mapping is as follows:
The function uses a static map for efficient type-to-color lookup and modifies the current OpenGL color state.
void setupCamera | ( | float | cx, |
float | cy, | ||
float | cz, | ||
float | radius, | ||
int | width, | ||
int | height | ||
) |
void setupLighting | ( | ) |
bool dragging = false |
Mouse drag state flags.
These track the current drag operation:
Mouse drag state flags.
double lastX |
Last recorded mouse cursor position.
Used to calculate mouse movement deltas for smooth camera control.
Last recorded mouse cursor position.
double lastY |
float panX = 0.0f |
Camera pan offset in X and Y directions.
These values control the 2D panning of the view:
Camera pan offset in X and Y directions.
float panY = 0.0f |
int renderMode = 1 |
Current rendering mode (1-5)
Controls how the neuron is rendered:
Current rendering mode (1-5)
bool rightDragging = false |
float rotateX = 0.0f |
Camera rotation around X and Y axes (in degrees)
These angles control the 3D view orientation:
Camera rotation around X and Y axes (in degrees)
float rotateY = 0.0f |
float zoom = 1.0f |
Camera zoom factor (higher values = more zoomed in)
Camera zoom factor (higher values = more zoomed in)