File size: 10,570 Bytes
fb867c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""
Optimized helix caching system for web deployment.

This module provides caching for helix geometry calculations to reduce
computational overhead in web deployments where the same helix configurations
are used repeatedly.

Key Features:
- Pre-computed position caches
- Memory-efficient storage
- Thread-safe access
- Automatic cache warming
"""

import numpy as np
import threading
import hashlib
import pickle
from typing import Dict, Tuple, Optional, List, Any
from dataclasses import dataclass
import logging

from core.helix_geometry import HelixGeometry

logger = logging.getLogger(__name__)


@dataclass
class HelixCacheEntry:
    """Cache entry for helix geometry data."""
    helix_params: Tuple[float, float, float, int]
    positions: np.ndarray  # Pre-computed positions
    radii: np.ndarray  # Pre-computed radii
    angles: np.ndarray  # Pre-computed angles
    access_count: int = 0
    last_accessed: float = 0.0


class HelixCache:
    """
    Caching system for helix geometry calculations.

    Provides fast access to pre-computed helix positions and properties
    to reduce computational overhead in web deployments.
    """

    def __init__(self, max_cache_size: int = 50, precompute_steps: int = 100):
        """
        Initialize helix cache.

        Args:
            max_cache_size: Maximum number of helix configurations to cache
            precompute_steps: Number of steps to precompute for each helix
        """
        self.max_cache_size = max_cache_size
        self.precompute_steps = precompute_steps
        self._lock = threading.Lock()
        self._cache: Dict[str, HelixCacheEntry] = {}

        # Pre-warm cache with common configurations
        self._warm_cache()

    def _warm_cache(self):
        """Pre-populate cache with common helix configurations."""
        common_configs = [
            # ComplexityLevel.DEMO
            (10.0, 0.01, 5.0, 5),
            # ComplexityLevel.SIMPLE
            (10.0, 0.01, 10.0, 10),
            # ComplexityLevel.MEDIUM
            (10.0, 0.01, 20.0, 20),
            # ComplexityLevel.COMPLEX
            (10.0, 0.01, 30.0, 30),
            # ComplexityLevel.RESEARCH (original)
            (33.0, 0.001, 33.0, 33),
        ]

        for config in common_configs:
            self._compute_and_cache(*config)

        logger.info(f"Cache warmed with {len(common_configs)} common configurations")

    def _make_cache_key(self, top_radius: float, bottom_radius: float,
                       height: float, turns: int) -> str:
        """Generate cache key from helix parameters."""
        params = f"{top_radius}_{bottom_radius}_{height}_{turns}"
        return hashlib.md5(params.encode()).hexdigest()

    def get_helix_data(self, top_radius: float, bottom_radius: float,
                      height: float, turns: int) -> Optional[HelixCacheEntry]:
        """
        Get cached helix data if available.

        Args:
            top_radius: Radius at the top of the helix
            bottom_radius: Radius at the bottom of the helix
            height: Total vertical height
            turns: Number of complete rotations

        Returns:
            Cached helix data or None if not cached
        """
        cache_key = self._make_cache_key(top_radius, bottom_radius, height, turns)

        with self._lock:
            if cache_key in self._cache:
                entry = self._cache[cache_key]
                entry.access_count += 1
                entry.last_accessed = time.time()

                # Move to end for LRU
                self._cache.move_to_end(cache_key)

                return entry

        # Not in cache, compute and store
        return self._compute_and_cache(top_radius, bottom_radius, height, turns)

    def _compute_and_cache(self, top_radius: float, bottom_radius: float,
                          height: float, turns: int) -> HelixCacheEntry:
        """Compute helix data and add to cache."""
        # Create helix geometry
        helix = HelixGeometry(top_radius, bottom_radius, height, turns)

        # Pre-compute positions at regular intervals
        t_values = np.linspace(0, 1, self.precompute_steps)
        positions = np.zeros((self.precompute_steps, 3))
        radii = np.zeros(self.precompute_steps)
        angles = np.zeros(self.precompute_steps)

        for i, t in enumerate(t_values):
            x, y, z = helix.get_position(t)
            positions[i] = [x, y, z]
            radii[i] = helix.get_radius(z)
            angles[i] = 2 * np.pi * turns * t

        # Create cache entry
        entry = HelixCacheEntry(
            helix_params=(top_radius, bottom_radius, height, turns),
            positions=positions,
            radii=radii,
            angles=angles
        )

        # Add to cache
        cache_key = self._make_cache_key(top_radius, bottom_radius, height, turns)

        with self._lock:
            # Evict oldest if at capacity
            if len(self._cache) >= self.max_cache_size:
                # Remove least recently used
                oldest_key = next(iter(self._cache))
                del self._cache[oldest_key]

            self._cache[cache_key] = entry

        logger.debug(f"Cached helix configuration: {turns} turns, {height} height")
        return entry

    def interpolate_position(self, entry: HelixCacheEntry, t: float) -> Tuple[float, float, float]:
        """
        Interpolate position from cached data.

        Args:
            entry: Cached helix data
            t: Parameter value (0-1)

        Returns:
            Interpolated (x, y, z) position
        """
        # Find surrounding cached points
        scaled_t = t * (self.precompute_steps - 1)
        idx_low = int(np.floor(scaled_t))
        idx_high = min(idx_low + 1, self.precompute_steps - 1)

        # Interpolation weight
        weight = scaled_t - idx_low

        # Linear interpolation
        pos_low = entry.positions[idx_low]
        pos_high = entry.positions[idx_high]

        interpolated = pos_low * (1 - weight) + pos_high * weight

        return tuple(interpolated)

    def get_cached_helix(self, top_radius: float, bottom_radius: float,
                        height: float, turns: int) -> 'CachedHelixGeometry':
        """
        Get a cached helix geometry object.

        Returns a helix-like object that uses cached data for fast lookups.
        """
        entry = self.get_helix_data(top_radius, bottom_radius, height, turns)
        return CachedHelixGeometry(self, entry)

    def get_statistics(self) -> Dict[str, Any]:
        """Get cache statistics."""
        with self._lock:
            total_accesses = sum(e.access_count for e in self._cache.values())
            avg_accesses = total_accesses / len(self._cache) if self._cache else 0

            return {
                "cache_size": len(self._cache),
                "max_size": self.max_cache_size,
                "total_accesses": total_accesses,
                "average_accesses": avg_accesses,
                "precompute_steps": self.precompute_steps
            }

    def clear(self):
        """Clear the cache."""
        with self._lock:
            self._cache.clear()
        logger.info("Helix cache cleared")


class CachedHelixGeometry:
    """
    Helix geometry wrapper that uses cached data.

    Provides the same interface as HelixGeometry but uses
    pre-computed cached data for fast lookups.
    """

    def __init__(self, cache: HelixCache, entry: HelixCacheEntry):
        """
        Initialize cached helix geometry.

        Args:
            cache: Parent cache instance
            entry: Cache entry with pre-computed data
        """
        self.cache = cache
        self.entry = entry

        # Extract parameters
        self.top_radius = entry.helix_params[0]
        self.bottom_radius = entry.helix_params[1]
        self.height = entry.helix_params[2]
        self.turns = entry.helix_params[3]

    def get_position(self, t: float) -> Tuple[float, float, float]:
        """
        Get position at parameter t using cached data.

        Args:
            t: Parameter value (0-1)

        Returns:
            (x, y, z) position
        """
        if not (0.0 <= t <= 1.0):
            raise ValueError("t must be between 0 and 1")

        return self.cache.interpolate_position(self.entry, t)

    def get_radius(self, z: float) -> float:
        """
        Get radius at height z using cached data.

        Args:
            z: Height value

        Returns:
            Radius at height z
        """
        # Convert z to t parameter
        t = 1.0 - (z / self.height) if self.height > 0 else 0.0
        t = max(0.0, min(1.0, t))

        # Interpolate from cached radii
        scaled_t = t * (len(self.entry.radii) - 1)
        idx_low = int(np.floor(scaled_t))
        idx_high = min(idx_low + 1, len(self.entry.radii) - 1)

        weight = scaled_t - idx_low
        return self.entry.radii[idx_low] * (1 - weight) + self.entry.radii[idx_high] * weight

    def get_velocity(self, t: float) -> Tuple[float, float, float]:
        """
        Get velocity vector at parameter t.

        Uses finite differences on cached data.
        """
        dt = 0.01
        t1 = max(0, t - dt / 2)
        t2 = min(1, t + dt / 2)

        pos1 = self.get_position(t1)
        pos2 = self.get_position(t2)

        actual_dt = t2 - t1
        if actual_dt > 0:
            velocity = tuple((p2 - p1) / actual_dt for p1, p2 in zip(pos1, pos2))
        else:
            velocity = (0.0, 0.0, 0.0)

        return velocity

    def calculate_arc_length(self, t1: float = 0.0, t2: float = 1.0,
                            steps: int = 100) -> float:
        """Calculate arc length between two parameter values."""
        # Use cached positions for fast calculation
        t_values = np.linspace(t1, t2, steps)
        total_length = 0.0

        for i in range(1, len(t_values)):
            pos1 = self.get_position(t_values[i - 1])
            pos2 = self.get_position(t_values[i])
            segment_length = np.sqrt(sum((p2 - p1) ** 2 for p1, p2 in zip(pos1, pos2)))
            total_length += segment_length

        return total_length


# Global cache instance
_global_helix_cache: Optional[HelixCache] = None


def get_helix_cache() -> HelixCache:
    """Get or create global helix cache instance."""
    global _global_helix_cache
    if _global_helix_cache is None:
        _global_helix_cache = HelixCache()
    return _global_helix_cache


# Import time at the end to avoid circular import
import time