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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import gradio as gr
import json, time, uuid, os, csv, random
import numpy as np

TRIALS_JSON = "trials.json"
LOG_PATH = "responses.csv"
# Check for persistent storage (Hugging Face Spaces)
if os.path.exists("/data"):
    LOG_PATH = "/data/responses.csv"

def load_trials(path=TRIALS_JSON):
    with open(path, "r") as f:
        return json.load(f)

TRIALS = load_trials()

def ensure_log_header():
    if not os.path.exists(LOG_PATH):
        with open(LOG_PATH, "w", newline="") as f:
            w = csv.writer(f)
            w.writerow([
                "ts", "participant_id", "trial_id", "task",
                "left_method", "right_method",
                "choice", "voxel_choice", "rt_ms",
                "prompt_text", "prompt_image"
            ])

ensure_log_header()

import os, hashlib
import trimesh

UNCOLORED_DIR = "cache_uncolored"
os.makedirs(UNCOLORED_DIR, exist_ok=True)

def to_uncolored_obj(mesh_path: str) -> str:
    # cache by path string (and you can include file mtime if you want)
    key = hashlib.md5(mesh_path.encode("utf-8")).hexdigest()
    out_path = os.path.join(UNCOLORED_DIR, f"{key}.glb")
    if os.path.exists(out_path):
        return out_path

    m = trimesh.load(mesh_path, force="mesh", process=False)

    # --- THIS is the key line: only verts + faces, no visual/material info ---
    # Preserving normals is crucial for correct shading
    m = trimesh.Trimesh(
        vertices=m.vertices, 
        faces=m.faces, 
        # vertex_normals=m.vertex_normals, 
        process=False
    )

    # export as OBJ (no textures/colors)
    m.export(out_path)
    return out_path

def to_gray_obj(mesh_path: str, gray=180) -> str:
    """
    Convert any mesh (obj/glb/gltf/...) to a geometry-only OBJ with constant gray vertex colors.
    gray: 0..255
    """
    # Updated cache key to 'v4_double_sided' to force regeneration
    key = hashlib.md5((mesh_path + f":g{gray}:v4_double_sided").encode("utf-8")).hexdigest()
    out_path = os.path.join(UNCOLORED_DIR, f"{key}.glb")
    if os.path.exists(out_path):
        return out_path

    # process=True fixes winding, merges vertices, etc.
    loaded = trimesh.load(mesh_path, process=True)

    # GLB/GLTF often loads as a Scene -> merge geometries
    if isinstance(loaded, trimesh.Scene):
        if len(loaded.geometry) == 0:
            raise ValueError(f"Empty scene: {mesh_path}")
        m = trimesh.util.concatenate(tuple(loaded.geometry.values()))
    else:
        m = loaded

    # Fix normals to ensure smooth shading (computes them if missing)
    # m.fix_normals()

    # Constant vertex colors (RGBA)
    vc = np.tile(np.array([gray, gray, gray, 255], dtype=np.uint8), (len(m.vertices), 1))
    m.visual = trimesh.visual.ColorVisuals(mesh=m, vertex_colors=vc)
    
    # Enable double-sided rendering to fix visibility issues
    # if hasattr(m.visual, 'material'):
        # m.visual.material.doubleSided = True
    # For some Trimesh versions/types, we might need to ensure it's PBR or handled correctly
    # But usually setting the attribute on the material object helps GLB export.

    # Export as GLB (supports vertex colors + normals well)
    m.export(out_path)
    return out_path

def to_ccm_obj(mesh_path: str, gray=180) -> str:
    """
    Convert any mesh (obj/glb/gltf/...) to a geometry-only OBJ with constant gray vertex colors.
    gray: 0..255
    """
    # Updated cache key to 'v4_double_sided' to force regeneration
    key = hashlib.md5((mesh_path + f":g{gray}:v4_ccm").encode("utf-8")).hexdigest()
    out_path = os.path.join(UNCOLORED_DIR, f"{key}.glb")
    if os.path.exists(out_path):
        return out_path

    # process=True fixes winding, merges vertices, etc.
    loaded = trimesh.load(mesh_path, process=True)

    # GLB/GLTF often loads as a Scene -> merge geometries
    if isinstance(loaded, trimesh.Scene):
        if len(loaded.geometry) == 0:
            raise ValueError(f"Empty scene: {mesh_path}")
        m = trimesh.util.concatenate(tuple(loaded.geometry.values()))
    else:
        m = loaded

    # Fix normals to ensure smooth shading (computes them if missing)
    # m.fix_normals()

    # Constant vertex colors (RGBA)
    # vc = np.tile(np.array([gray, gray, gray, 255], dtype=np.uint8), (len(m.vertices), 1))
    num_vertices = len(m.vertices)
    # color based on coord
    vc = m.vertices.copy()
    vc = (vc - vc.min(axis=0)) / (vc.max(axis=0) - vc.min(axis=0))
    vc = (vc * 255).astype(np.uint8)
    vc_alpha = np.ones((num_vertices, 1), dtype=np.uint8) * 255
    vc = np.concatenate([vc, vc_alpha], axis=1)
    # normalize vertices to 0,1

    m.visual = trimesh.visual.ColorVisuals(mesh=m, vertex_colors=vc)
    
    # Enable double-sided rendering to fix visibility issues
    # if hasattr(m.visual, 'material'):
        # m.visual.material.doubleSided = True
    # For some Trimesh versions/types, we might need to ensure it's PBR or handled correctly
    # But usually setting the attribute on the material object helps GLB export.

    # Export as GLB (supports vertex colors + normals well)
    m.export(out_path)
    return out_path

def to_normal_obj(mesh_path: str, gray=180) -> str:
    """
    Convert any mesh (obj/glb/gltf/...) to a geometry-only OBJ with constant gray vertex colors.
    gray: 0..255
    """
    # Updated cache key to 'v4_double_sided' to force regeneration
    key = hashlib.md5((mesh_path + f":g{gray}:v4_normal").encode("utf-8")).hexdigest()
    out_path = os.path.join(UNCOLORED_DIR, f"{key}.glb")
    if os.path.exists(out_path):
        return out_path

    # process=True fixes winding, merges vertices, etc.
    loaded = trimesh.load(mesh_path, process=True)

    # GLB/GLTF often loads as a Scene -> merge geometries
    if isinstance(loaded, trimesh.Scene):
        if len(loaded.geometry) == 0:
            raise ValueError(f"Empty scene: {mesh_path}")
        m = trimesh.util.concatenate(tuple(loaded.geometry.values()))
    else:
        m = loaded

    # Fix normals to ensure smooth shading (computes them if missing)
    m.fix_normals()

    # Normalize normals to 0..1 range for visualization
    # Normal range is [-1, 1], so (n + 1) / 2 maps to [0, 1]
    vc = (m.vertex_normals + 1) / 2
    vc = (vc * 255).astype(np.uint8)
    
    # Add alpha channel
    num_vertices = len(m.vertices)
    vc_alpha = np.ones((num_vertices, 1), dtype=np.uint8) * 255
    vc = np.concatenate([vc, vc_alpha], axis=1)

    m.visual = trimesh.visual.ColorVisuals(mesh=m, vertex_colors=vc)
    
    # Enable double-sided rendering to fix visibility issues
    # if hasattr(m.visual, 'material'):
        # m.visual.material.doubleSided = True
    # For some Trimesh versions/types, we might need to ensure it's PBR or handled correctly
    # But usually setting the attribute on the material object helps GLB export.

    # Export as GLB (supports vertex colors + normals well)
    m.export(out_path)
    return out_path

def _trial_to_view(trial, flip_lr: bool):
    if flip_lr:
        left_method, right_method = "continuous", "ours"
        left_voxel, right_voxel = trial["cont_voxel"], trial["ours_voxel"]
        left_mesh,  right_mesh  = trial["cont_mesh"],  trial["ours_mesh"]
        left_video = trial.get("cont_video", None)
        right_video = trial.get("ours_video", None)
    else:
        left_method, right_method = "ours", "continuous"
        left_voxel, right_voxel = trial["ours_voxel"], trial["cont_voxel"]
        left_mesh,  right_mesh  = trial["ours_mesh"],  trial["cont_mesh"]
        left_video = trial.get("ours_video", None)
        right_video = trial.get("cont_video", None)

    # Make sure voxel OBJ is geometry-only (and optionally also strip mesh if you want)
    left_voxel  = to_ccm_obj(left_voxel)
    right_voxel = to_ccm_obj(right_voxel)

    # (Optional) also strip mesh to geometry-only if needed:
    left_mesh  = to_normal_obj(left_mesh)   # only if you want to force mesh untextured via OBJ export
    right_mesh = to_normal_obj(right_mesh)

    task = trial["task"]
    prompt_text = trial.get("prompt_text", "")
    prompt_image = trial.get("prompt_image", None)

    show_text = (task == "text")
    show_img = (task == "image")

    return dict(
        trial_id=trial["trial_id"],
        task=task,
        show_text=show_text,
        show_img=show_img,
        prompt_text=prompt_text,
        prompt_image=prompt_image,
        left_method=left_method,
        right_method=right_method,
        left_voxel=left_voxel,
        right_voxel=right_voxel,
        left_mesh=left_mesh,
        right_mesh=right_mesh,
        left_video=left_video,
        right_video=right_video,
    )

def start_session(seed=None):
    pid = str(uuid.uuid4())[:8]
    rng = random.Random(seed if seed is not None else time.time())

    order = list(range(len(TRIALS)))
    rng.shuffle(order)

    # DEBUG: move specific trial to front
    # target_id = "img_treehouse_rmapple"
    # for i, t in enumerate(TRIALS):
    #     if t["trial_id"] == target_id:
    #         if i in order:
    #             order.remove(i)
    #             order.insert(0, i)
    #         break

    state = {
        "pid": pid,
        "order": order,
        "i": 0,
        "t0": time.time(),
        "flip_lr": rng.choice([False, True]),  # for first trial
        "rng_state": rng.getstate(),
    }

    view = _trial_to_view(TRIALS[order[0]], state["flip_lr"])
    return (
        state, pid,
        gr.update(value=f"Trial 1 / {len(TRIALS)}"),
        gr.update(visible=view["show_text"], value=view["prompt_text"]),
        gr.update(visible=view["show_img"], value=view["prompt_image"]),
        gr.update(value="Left Model"),
        gr.update(value="Right Model"),
        view["left_voxel"], view["right_voxel"],
        view["left_mesh"], view["right_mesh"],
        view["left_video"], view["right_video"],
        gr.update(value=""),  # completion code
        gr.update(value=None), # reset choice
        gr.update(value=None)  # reset voxel_choice
    )

def submit(choice, voxel_choice, state):
    i = state["i"]
    idx = state["order"][i]
    trial = TRIALS[idx]
    rt_ms = int((time.time() - state["t0"]) * 1000)

    # log
    with open(LOG_PATH, "a", newline="") as f:
        w = csv.writer(f)
        w.writerow([
            time.time(), state["pid"], trial["trial_id"], trial["task"],
            ("continuous" if state["flip_lr"] else "ours"),
            ("ours" if state["flip_lr"] else "continuous"),
            choice, voxel_choice, rt_ms,
            trial.get("prompt_text", ""),
            trial.get("prompt_image", ""),
        ])

    # advance
    state["i"] += 1
    if state["i"] >= len(TRIALS):
        code = f"COMPLETE-{state['pid']}"
        return (
            state,
            gr.update(value="Done ✅ (copy the completion code below)"),
            gr.update(visible=False, value=""),
            gr.update(visible=False, value=None),
            gr.update(value="LEFT"),
            gr.update(value="RIGHT"),
            None, None,
            None, None,
            None, None,
            gr.update(value=code),
            gr.update(value=None),
            gr.update(value=None)
        )

    # next trial
    rng = random.Random()
    rng.setstate(state["rng_state"])
    state["flip_lr"] = rng.choice([False, True])
    state["rng_state"] = rng.getstate()
    state["t0"] = time.time()

    ni = state["i"]
    nidx = state["order"][ni]
    view = _trial_to_view(TRIALS[nidx], state["flip_lr"])

    return (
        state,
        gr.update(value=f"Trial {ni+1} / {len(TRIALS)}"),
        gr.update(visible=view["show_text"], value=view["prompt_text"]),
        gr.update(visible=view["show_img"], value=view["prompt_image"]),
        gr.update(value="Left Model"),
        gr.update(value="Right Model"),
        view["left_voxel"], view["right_voxel"],
        view["left_mesh"], view["right_mesh"],
        view["left_video"], view["right_video"],
        gr.update(value=""),
        gr.update(value=None),
        gr.update(value=None)
    )

def get_responses_file(password):
    # Set your password in HF Space Settings as ADMIN_PASSWORD, or use default "123456"
    correct_pass = os.environ.get("ADMIN_PASSWORD", "123456")
    if password != correct_pass:
        raise gr.Error("Incorrect Password")
    
    if not os.path.exists(LOG_PATH):
        raise gr.Error("No responses collected yet.")
        
    return LOG_PATH

def clear_responses(password):
    correct_pass = os.environ.get("ADMIN_PASSWORD", "123456")
    if password != correct_pass:
        raise gr.Error("Incorrect Password")
        
    if os.path.exists(LOG_PATH):
        os.remove(LOG_PATH)
        ensure_log_header() # Re-create header immediately
        return "Responses cleared successfully."
    else:
        return "No responses file found to clear."

with gr.Blocks(css="""
#prompt-img {max-width: 320px; margin: 0 auto;}
#prompt-img img {width: 100% !important; height: auto !important;}
.model3d-window {height: 600px !important;}
""") as demo:
    st = gr.State()

    gr.Markdown("## 3D Asset User Study (Image + Text)")

    with gr.Accordion("📝 Instructions (Click to Read)", open=True):
        gr.Markdown("""
        ### How to complete this study:
        1. **Observe the Prompt**: You will see a text description or a reference image.
        2. **Compare Models**: Rotate and zoom into the "Left" and "Right" 3D models to inspect them.
        3. **Vote**: Select which model, voxel (Geometry and/or Texture) better matches the prompt or has higher quality. 
        4. **Submit**: Click "Submit" to move to the next trial.
        
        *You could consider:
        1. Alignment with prompt.
        2. Stuctural quality, such as holes, cracks, floaters, etc.
        3. Since the process of voxel->mesh can be lossy (missing parts), please also inspect voxels' quality.
        4. If you are not sure about the winner, pick not sure.
        5. Unexpected holes on voxels are typically not desired.*
        """)

    pid_box = gr.Textbox(label="Participant ID (auto)", interactive=False)
    progress = gr.Markdown()

    # Prompt area
    prompt_text = gr.Markdown(visible=False)
    prompt_img = gr.Image(label="Prompt Image", visible=False, elem_id="prompt-img")

    gr.Markdown("### Compare the two methods")

    with gr.Row():
        with gr.Column():
            left_title = gr.Textbox(label="Left Method", interactive=False)
            left_voxel = gr.Model3D(label="Left Voxel (untextured)", clear_color=[1,1,1,1], elem_classes="model3d-window")
            left_mesh  = gr.Model3D(label="Left Mesh (untextured)",  clear_color=[1,1,1,1], elem_classes="model3d-window")
            left_vid   = gr.Video(label="Left Video (optional)", autoplay=True)

        with gr.Column():
            right_title = gr.Textbox(label="Right Method", interactive=False)
            right_voxel = gr.Model3D(label="Right Voxel (untextured)", clear_color=[1,1,1,1], elem_classes="model3d-window")
            right_mesh  = gr.Model3D(label="Right Mesh (untextured)",  clear_color=[1,1,1,1], elem_classes="model3d-window")
            right_vid   = gr.Video(label="Right Video (optional)", autoplay=True)

    voxel_choice = gr.Radio(["Left", "Right", "Not Sure"], label="Which generated voxel is better?", value=None)
    choice = gr.Radio(["Left", "Right", "Not Sure"], label="Which generated mesh is better?", value=None)
    
    btn = gr.Button("Submit")

    code_box = gr.Textbox(label="Completion Code", interactive=False)

    demo.load(
        start_session,
        inputs=None,
        outputs=[st, pid_box, progress, prompt_text, prompt_img,
                left_title, right_title,
                left_voxel, right_voxel, left_mesh, right_mesh, left_vid, right_vid,
                code_box, choice, voxel_choice]
    )

    btn.click(
        submit,
        inputs=[choice, voxel_choice, st],
        outputs=[st, progress, prompt_text, prompt_img,
                left_title, right_title,
                left_voxel, right_voxel, left_mesh, right_mesh, left_vid, right_vid,
                code_box, choice, voxel_choice]
    )

    gr.Markdown("---")
    with gr.Accordion("Admin Zone (Download Responses)", open=False):
        with gr.Row():
            admin_pass = gr.Textbox(label="Admin Password", type="password", placeholder="Enter password to download")
            download_btn = gr.Button("Download CSV")
            clear_btn = gr.Button("Clear Responses", variant="stop")
        
        with gr.Row():
            admin_file = gr.File(label="Responses File", interactive=False)
            admin_status = gr.Textbox(label="Status", interactive=False)

        download_btn.click(
            get_responses_file,
            inputs=[admin_pass],
            outputs=[admin_file]
        )
        
        clear_btn.click(
            clear_responses,
            inputs=[admin_pass],
            outputs=[admin_status]
        )

demo.launch(allowed_paths=["/data"])